void swap(int &first, int &second){
int temp = first;
first = second;
second = temp;
}
Run Code Online (Sandbox Code Playgroud)
//////
int a=3,b=2;
swap(a,b);
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,C编译器抱怨"void swap(int&first,int&second)"在"(/ {"之前有一个语法错误,如缺少"&".
我不明白为什么?C不支持此功能吗?
我是C#的新手.我用C#中的参数尝试了这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class First
{
public void fun(out int m)
{
m *= 10;
Console.WriteLine("value of m = " + m);
}
}
class Program
{
static void Main(string[] args)
{
First f = new First();
int x = 30;
f.fun(out x);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一些错误,如" 使用未分配的参数'm'",并且
必须在控制离开当前方法之前指定out参数'm'.
那么这些错误的含义是什么呢?为什么在我已经为x赋值时必须分配' m ' .
在C#PInvoke中,如何传递字符串缓冲区以便C DLL填充它并返回?PInvoke宣言是什么?
C函数声明是
int GetData(char* data, int buflength);
Run Code Online (Sandbox Code Playgroud)
在C#中,我已将其声明为
[DllImport(DllName)]
static extern Int32 GetData([MarshalAs(UnmanagedType.LPStr)]StringBuilder receiveddata, Int32 buflen);
Run Code Online (Sandbox Code Playgroud)
这是对的吗?我像这样传递StringBuilder变量
int bufferLength = 32;
StringBuilder data = new StringBuilder(bufferLength);
int result = GetData(data, bufferLength);
Run Code Online (Sandbox Code Playgroud)
我想知道它是否正确?
谢谢
我只想从PowerShell 调用Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator的GenerateScript方法.
#C
public void GenerateScript(
IScriptFragment scriptFragment,
out string script
)
Run Code Online (Sandbox Code Playgroud)
我找到了这个,但我没有得到它的工作
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator
$sql = 'select * from PowerShell'
$out = ''
$sg.GenerateScript($sql, [ref] $out)
$out
Run Code Online (Sandbox Code Playgroud)
这给了
Cannot find an overload for "GenerateScript" and the argument count: "2".
At line:6 char:19
+ $sg.GenerateScript <<<< ($sql, [ref] $out)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Run Code Online (Sandbox Code Playgroud)
编辑:
目前的版本是
$sql = 'select * from PowerShell'
$sr = new-Object System.IO.StringReader($sql) …Run Code Online (Sandbox Code Playgroud) VB.NET没有out参数,但您可以<Out()> ByRef在COM和P/Invoke方法上指定以获得与外部方法相同的效果.
在内部方法上指定相同的方法(即仅由.NET代码调用的方法)实际上是否有助于Jitter(或VB.NET编译器)?或者它目前仅作为程序员注释有用.
它是否有可能在未来的Jitter中使用,或者在编译时这个属性是否会丢失?
所以我是C#的新手,我很难理解out.而不是仅仅从函数返回一些东西
using System;
class ReturnTest
{
static double CalculateArea()
{
double r=5;
double area = r * r * Math.PI;
return area;
}
static void Main()
{
double output = CalculateArea();
Console.WriteLine("The area is {0:0.00}", output);
}
}
Run Code Online (Sandbox Code Playgroud)
与此相比
using System;
class ReturnTest
{
static void CalculateArea(out double r)
{
r=5;
r= r * r * Math.PI;
}
static void Main()
{
double radius;
CalculateArea(out radius);
Console.WriteLine("The area is {0:0.00}",radius );
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
第一个是我一般会这样做的.我有可能想要使用out而不仅仅是返回语句吗?我理解ref …
目前,当尝试在带有out参数的方法中执行某些操作时,我需要在方法体中指定out参数的值,例如
public static void TryDoSomething(int value, out bool itWorkerd)
{
itWorkerd = true;
if (someFavourableCondition)
{
// if I didn't assign itWorked variable before this point,
// I get an error: "Parameter itWorked must be assigned upon exit.
return;
}
// try to do thing
itWorkerd = // success of attempt to do thing
}
Run Code Online (Sandbox Code Playgroud)
我希望能够设置itWorked参数的默认值,所以我不必在方法体中任意设置值.
public static void TryDoSomething(int value, out bool itWorkerd = true)
{
if (someFavourableCondition)
{
// itWorked was already assigned with a default …Run Code Online (Sandbox Code Playgroud) 按值传递意味着传递参数的副本.对该副本的更改不会更改原始副本.
通过引用传递意味着传递对原始的引用.对引用的更改会影响原始引用.
REF告诉编译器在进入函数之前初始化对象.REF表示该值已经设置,因此该方法可以读取并修改它.REF有两种方式,包括进出.
OUT告诉编译器该对象将在函数内初始化.OUT表示该值尚未设置,因此必须在调用return之前设置.OUT只是一种方式,即出局.
那么在什么情况下你会结合使用ref和out关键字,通过引用传递还是通过值传递?例子会有很大帮助.
非常感谢.
在做类似的事情时:
int value;
if (dict.TryGetValue(key, out value))
{
if (condition)
{
//value = 0; this copies by value so it doesn't change the existing value
dict[key] = 0;
}
}
else
{
dict[key] = 0;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以避免索引查找来替换现有值吗?我已经使用TryGetValue验证密钥存在,因此再次通过索引检索值似乎是浪费.
另外,在我的代码的else {}部分中,通常认为在添加新值或替换旧值时使用索引器是一种好习惯,并添加以明确您是在添加而不是替换?或者我应该每次只使用索引器?我学习使用字典的方式,我总是做一个TryGetValue查找,而在else部分我处理没有键存在的情况.
我想验证一个方法只被调用一次。
mock.Verify(x => x.Method("String", out It.IsAny<StringBuilder>()), Times.Once);
Run Code Online (Sandbox Code Playgroud)
我不关心第二个参数,它可以是任何东西。
由于该参数,我收到以下错误消息out:
“out”参数必须是可分配的变量、字段或数组元素
out ×10
c# ×6
.net ×1
byref ×1
c ×1
dictionary ×1
moq ×1
parameters ×1
pinvoke ×1
powershell ×1
ref ×1
reference ×1
return ×1
trygetvalue ×1
unit-testing ×1
vb.net ×1