标签: out

Console.ReadLine将48添加到int

当我向ReadLine()输入0时,我得到48.

这是一个错误吗?

class Program
{
    static void Main(string[] args)
    {
        string name;
        int age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out int age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

c# out

2
推荐指数
2
解决办法
3680
查看次数

C++ ios :: out文件流标志:为什么它会影响性能?

我的程序正在将uint16_t的大数字(每次250,000)写入文件.出于某种原因,设置ios :: out标志(不必要,因为VS2010会自动设置它)导致性能下降大约10倍.(见前/后).知道设置那个可能导致如此巨大性能差异的标志是什么意思吗?

之前:

fileoutput.flags(ios::out); 
Run Code Online (Sandbox Code Playgroud)

之前

后:

//fileoutput.flags(ios::out);  
Run Code Online (Sandbox Code Playgroud)

后

c++ out filestream ios

2
推荐指数
1
解决办法
341
查看次数

在类属性上使用out参数

有没有办法out在未初始化的对象属性上使用?

例如:

QuoteDetail q = new QuoteDetail();

Dictionary<int, string> messageDict = SplitMessage(msg);

messageDict.TryGetValue(8, out q.QuoteID); //doesn't work
Run Code Online (Sandbox Code Playgroud)

c# parameters properties out parameter-passing

2
推荐指数
1
解决办法
167
查看次数

抛出异常而不设置参数时的行为是什么?

在设置 out 参数的值之前引发异常,然后尝试访问该参数时,C# 中定义的行为是什么?

public void DoSomething(int id, out control)
{
    try
    {
        int results = Call(Id);
        control = new CallControl(results);
    }
    catch(Exception e)
    {
      throw new Exception("Call failed", e);
    }
}

//somewhere else
DoSomething(out Control control)
{
    try
    {
       DoSomething(1, out control);
    }
    catch()
    {
    // handle exception
    }
}

// later
Control control;
DoSomething(out control)
control.Show();
Run Code Online (Sandbox Code Playgroud)

编译器通常会抱怨在设置 out 参数之前退出该方法。这似乎比它更聪明,无法保护我免受自己的伤害。

c# exception out

2
推荐指数
1
解决办法
731
查看次数

ref 和 out 值类型变量

out 上的 msdn 文档说作为 out 传递的参数必须在函数内部分配一个值。来自网站的示例:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
       // value is now 44
    }
 }
Run Code Online (Sandbox Code Playgroud)

根据我的理解,当声明 int "value" 时,它已经被分配了一个默认值 0(因为 int 是一个值类型并且不能为 null。)那么为什么有必要让 "Method" 修改它的值呢?

同样,如果使用“ref”而不是“out”,是否还需要初始化“value”?

有这样的问题'ref' 和 'out' 关键字什么区别?但没有人想把 2 和 2 放在一起。

c# methods ref out

2
推荐指数
1
解决办法
892
查看次数

我想用网络浏览器控制Arduino的数字输出端口.我该怎么办?

我使用aduino的输出端口控制设备.但我想通过Internet控制它.如何使用Ethernet Shield创建和控制网页?

port arduino out digital web

2
推荐指数
1
解决办法
144
查看次数

调用者的"out"关键字的目的是什么(在C#中)?

当C#函数具有输出参数时,您可以按如下方式清除:

private void f(out OutputParameterClass outputParameter);
Run Code Online (Sandbox Code Playgroud)

这表明在调用函数时不必初始化参数.但是,在调用此函数时,您必须重复out关键字:

f(out outputParameter);
Run Code Online (Sandbox Code Playgroud)

我想知道这有什么好处.为什么有必要重复部分功能规范?有人知道吗?

c# parameters ref out

1
推荐指数
1
解决办法
778
查看次数

在Scala中模仿C#out和ref - 准备使用功能?

在有限的意义上,你自己编写out和编写它很容易ref,但我的问题不是如何做 - 但是有一些功能(或类)可以使用吗?

我发现最接近的是Reference特质(但它是一种特质).

我需要那些,而不是元组,不是选项,而不是纯粹的结果,因为只有ref/out才能使链接优雅.

scala ref out

1
推荐指数
1
解决办法
736
查看次数

在指针参数上使用ref和out关键字

我正在学习C和C#,这个问题适用于C#.你为什么要在指针上使用out和ref关键字?使用指针,您可以直接访问变量.我在msdn上找到了这个代码:here.

这是代码:

static int value = 20;
public unsafe static void F(out int* pi1, ref int* pi2)
{
  int i = 10;
  pi1 = &i;
  fixed (int* pj = &value)
  {
     // ...
     pi2 = pj;
  }
}
Run Code Online (Sandbox Code Playgroud)

c# pointers ref out

1
推荐指数
1
解决办法
431
查看次数

在初始化之前比较参数

我尝试比较字符串变量和字符串输出参数,它不编译.它似乎等待比较前的初始化,但我不能改变之前的值.

public bool DownloadZipFile(out string zipUrl)
{
        string zip = System.Windows.Forms.Clipboard.GetText();
        // my code ...

        if (zipUrl != string.Empty && zipUrl == zip)
            Assert.Fail("Copy Zip Url : zip url not updated . zip url equal to prev zip url");

        zipUrl = zip;
        return true;
}
Run Code Online (Sandbox Code Playgroud)

当我建立项目时,我得到:

错误2使用未分配的输出参数'zipUrl'

c# out

1
推荐指数
1
解决办法
87
查看次数