我们假设我有一个带out参数的函数,但是我不需要它的值.如果给定结果将丢弃,有没有办法传递没有实际参数?
编辑:
虽然这个问题已被选为可选输出参数的重复, 但只有从方法创建者的角度来看才是这样.如果您是该方法的用户,那么您不想让参数成为可选参数,只是在不声明变量的情况下不使用它.虽然这是不可能的,但使用C#7.0可以在方法调用中声明它.所以代替:
int unusedValue;
TryGetValue("key", out unusedValue);
Run Code Online (Sandbox Code Playgroud)
你得到:
TryGetValue("key", out int unusedValue);
Run Code Online (Sandbox Code Playgroud)
甚至:
TryGetValue("key", out _);
Run Code Online (Sandbox Code Playgroud)
这应该作为答案添加,但是:
我编写了函数'A',它将调用许多其他函数之一.为了保存重写函数'A',我想传递函数作为函数'A'的参数调用.例如:
function A{
Param($functionToCall)
Write-Host "I'm calling : $functionToCall"
}
function B{
Write-Host "Function B"
}
Function C{
write-host "Function C"
}
A -functionToCall C
Run Code Online (Sandbox Code Playgroud)
返回:我正在打电话:C
我期待它回归:我在呼唤:功能C.
我尝试了各种各样的东西,比如:
Param([scriptblock]$functionToCall)
Run Code Online (Sandbox Code Playgroud)
无法将System.String转换为ScriptBlock
A -functionToCall $function:C
Run Code Online (Sandbox Code Playgroud)
返回"写主机"功能C"
A - functionToCall (&C)
Run Code Online (Sandbox Code Playgroud)
这在其余部分之前进行评估:
Function C
I'm Calling :
Run Code Online (Sandbox Code Playgroud)
我确定这是编程101,但我无法弄清楚正确的语法或它是什么我做错了.我们将非常感激地提供任何帮助.非常感谢.
我想传递arrange(){dplyr}一个变量名的向量来排序.通常我只需输入我想要的变量,但我正在尝试创建一个函数,其中排序变量可以作为函数参数输入.
df <- structure(list(var1 = c(1L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 4L, 4L
), var2 = structure(c(10L, 1L, 8L, 3L, 5L, 4L, 7L, 9L, 2L, 6L
), .Label = c("b", "c", "f", "h", "i", "o", "s", "t", "w", "x"
), class = "factor"), var3 = c(7L, 5L, 5L, 8L, 5L, 8L, 6L, 7L,
5L, 8L), var4 = structure(c(8L, 5L, 1L, 4L, 7L, 4L, 3L, 6L, 9L,
2L), .Label = c("b", "c", "d", "e", "f", "h", "i", …Run Code Online (Sandbox Code Playgroud) 代码优先.这就是我想要做的.我很接近,但我想我只需要修改我在UpdateButton方法中定义参数的方式.
private async void UpdateButton(Action<bool> post)
{
if (!await post())
ErrorBox.Text = "Error posting message.";
}
private void PostToTwitter()
{
UpdateButton(async () => await new TwitterAction().Post("Hello, world!"));
}
private void PostToFacebook()
{
UpdateButton(async () => await new FacebookAction().Post("Hello, world!"));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这!await post()不起作用,因为"Type'void'不是等待的." 所以问题是,如何在此方法中定义我的参数以支持等待参数?
这是TwitterAction().Post()的定义方式......
public virtual async Task<bool> Post(string messageId){...}
这2个代码有什么区别:
代码A:
Foo myFoo;
myFoo = createfoo();
Run Code Online (Sandbox Code Playgroud)
哪里
public Foo createFoo()
{
Foo foo = new Foo();
return foo;
}
Run Code Online (Sandbox Code Playgroud)
比.代码B:
Foo myFoo;
createFoo(myFoo);
public void createFoo(Foo foo)
{
Foo f = new Foo();
foo = f;
}
Run Code Online (Sandbox Code Playgroud)
这2个代码之间有什么区别吗?
java terminology parameter-passing pass-by-reference pass-by-value
在c ++ 11中传递lambda非常简单:
func( []( int arg ) {
// code
} ) ;
Run Code Online (Sandbox Code Playgroud)
但我想知道,将lambda传递给像这样的函数的成本是多少?如果func将lambda传递给其他函数怎么办?
void func( function< void (int arg) > f ) {
doSomethingElse( f ) ;
}
Run Code Online (Sandbox Code Playgroud)
lambda的传递是否昂贵?由于可以为function对象分配0,
function< void (int arg) > f = 0 ; // 0 means "not init"
Run Code Online (Sandbox Code Playgroud)
它让我认为函数对象就像指针一样.但是,如果不使用new,则意味着它们可能类似于值类型struct或类,它们默认为堆栈分配和成员方式副本.
当你按"值"传递一个函数对象时,C++ 11"代码体"和捕获的变量组是如何传递的?是否有很多代码体的多余副本?我是否必须标记function传递的每个对象,const&以便不进行复制:
void func( const function< void (int arg) >& f ) {
}
Run Code Online (Sandbox Code Playgroud)
或者以某种方式使函数对象以不同于常规C++结构的方式传递?
所以我试图使用razor'@'表示法(在MVC-4视图中)将参数传递给javascript函数但是我收到语法错误:"未终止的字符串常量"
还有另一种方法我应该写这个吗?
@foreach (var item in Model)
{
...
<input type="button" value="Assign" onclick="AssignButtonClicked('@item.ID')" />
}
Run Code Online (Sandbox Code Playgroud)
我使用了与此答案相同的语法/sf/answers/362552151/
编辑:
这只是一个语法错误,功能仍然有效
javascript asp.net-mvc parameter-passing razor asp.net-mvc-4
我有一个网址:foo.php?name=adam&lName=scott,和foo.php我有一个形式,它给我的价值观rectangleLength和rectangleBreadth与一个提交按钮.
当我单击带有表单操作的提交按钮时$_SERVER['REQUEST_URI'],我得到了这个结果URL :( foo.php?rectangleLength=10&rectangleBreadth=5这些值已由用户填写).
请注意,我正在丢失以前的值name和lNameURL.
我怎么能保留它们?
另外,请记住,我必须返回foo.php并且如果用户想要再次提交表单,则长度和宽度值应该更改.
是否可以将OrderedDict实例传递给使用**kwargs语法并保留排序的函数?
我想做的是:
def I_crave_order(**kwargs):
for k, v in kwargs.items():
print k, v
example = OrderedDict([('first', 1), ('second', 2), ('third', -1)])
I_crave_order(**example)
>> first 1
>> second 2
>> third -1
Run Code Online (Sandbox Code Playgroud)
但实际结果是:
>> second 2
>> third -1
>> first 1
Run Code Online (Sandbox Code Playgroud)
即,典型的随机字典排序.
我有其他用途明确设置顺序是好的,所以我想保留**kwargs而不是仅仅将OrderedDict作为常规参数传递
在iOS 8中,我有以下功能正常运行:
func showConfirmBox(msg:String, title:String,
firstBtnStr:String,
secondBtnStr:String,
caller:UIViewController) {
let userPopUp = UIAlertController(title:title,
message:msg, preferredStyle:UIAlertControllerStyle.Alert)
userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,
handler:{action in}))
userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,
handler:{action in}))
caller.presentViewController(userPopUp, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
我想做类似下面的事情,以便传递当要触摸一个或另一个按钮时要执行的方法作为参数:
func showConfirmBox(msg:String, title:String,
firstBtnStr:String, firstSelector:Selector,
secondBtnStr:String, secondSelector:Selector,
caller:UIViewController) {
let userPopUp = UIAlertController(title:title,
message:msg, preferredStyle:UIAlertControllerStyle.Alert)
userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,
handler:{action in caller.firstSelector()}))
userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,
handler:{action in caller.secondSelector()}))
caller.presentViewController(userPopUp, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
显然我没有使用firstSelector和secondSelector做正确的事情,因为我到目前为止所做的尝试都没有用.我想我没有使用正确的语法来满足我的需求,但我确信我可以做我想做的事情.知道如何正确地做到这一点?
c# ×2
lambda ×2
asp.net-mvc ×1
asynchronous ×1
c++ ×1
c++11 ×1
dplyr ×1
forms ×1
function ×1
get ×1
ios ×1
java ×1
javascript ×1
kwargs ×1
php ×1
powershell ×1
python ×1
r ×1
razor ×1
sorting ×1
std-function ×1
swift ×1
terminology ×1