C#:在单个语句中为多个变量分配相同的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1419252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:19:37  来源:igfitidea点击:

C#: Assign same value to multiple variables in single statement

c#

提问by Alex

Is there any way (just out of curiosity because I came across multiple same-value assignments to multiple variables today) in C# to assign one value to multiple variables at once in a single statements?

有没有什么办法(只是出于好奇,因为我今天遇到了对多个变量的多个相同值赋值)在 C# 中在单个语句中一次将一个值分配给多个变量?

Something along these lines (pseudocode):

沿着这些路线的东西(伪代码):

int num1 = 1;
int num2 = 1;

num1 & num2 = 5;

Probably not but I thought it was worth asking in case something similar is actually possible!

可能不是,但我认为值得一问,以防类似的事情实际上是可能的!

采纳答案by Pierre-Alain Vigeant

It's as simple as:

这很简单:

num1 = num2 = 5;

When using an object property instead of variable, it is interesting to know that the getaccessor of the intermediate value is not called. Only the setaccessor is invoked for all property accessed in the assignation sequence.

当使用对象属性而不是变量时,有趣的是知道get中间值的访问器没有被调用。set对于在分配序列中访问的所有属性,仅调用访问器。

Take for example a class that write to the console everytime the getand setaccessor are invoked.

以每次调用getset访问器时写入控制台的类为例。

static void Main(string[] args)
{
    var accessorSource = new AccessorTest(5);
    var accessor1 = new AccessorTest();
    var accessor2 = new AccessorTest();

    accessor1.Value = accessor2.Value = accessorSource.Value;

    Console.ReadLine();
}

public class AccessorTest
{
    public AccessorTest(int value = default(int))
    {
        _Value = value;
    }

    private int _Value;

    public int Value
    {
        get
        {
            Console.WriteLine("AccessorTest.Value.get {0}", _Value);
            return _Value;
        }
        set
        {
            Console.WriteLine("AccessorTest.Value.set {0}", value);
            _Value = value;
        }
    }
}

This will output

这将输出

AccessorTest.Value.get 5
AccessorTest.Value.set 5
AccessorTest.Value.set 5

Meaning that the compiler will assign the value to all properties and it will not re-read the value every time it is assigned.

这意味着编译器会将值分配给所有属性,并且不会在每次分配时重新读取该值。

回答by Daniel

num1 = num2 = 5

数量 1 = 数量 2 = 5

回答by LukeH

int num1, num2, num3;

num1 = num2 = num3 = 5;

Console.WriteLine(num1 + "=" + num2 + "=" + num3);    // 5=5=5

回答by Rohan West

Something like this.

像这样的东西。

num1 = num2 = 5

回答by SLaks

Try this:

尝试这个:

num1 = num2 = 5;

Note that this won't work in VB.

请注意,这在 VB 中不起作用。

回答by Druid

Your example would be:

你的例子是:

int num1 = 1;
int num2 = 1;

num1 = num2 = 5;

回答by aaronb

This will do want you want:

这确实需要你想要的:

int num1, num2;
num1 = num2 = 5;

'num2 = 5' assignment will return the assigned value.

'num2 = 5' 赋值将返回分配的值。

This allows you to do crazy things like num1 = (num2 = 5) +3;which will assign 8 to num1, although I would not recommended doing it as not be very readable.

这允许你做一些疯狂的事情,比如num1 = (num2 = 5) +3;将 8 分配给 num1,尽管我不建议这样做,因为它不太可读。

回答by Zain Ali

int num1=5,num2=5

Declaring and assigning variables in the same statement.

在同一个语句中声明和分配变量。

回答by abuss

Something a little shorter in syntax but taking what the others have already stated.

语法稍短,但采用其他人已经说过的内容。

int num1, num2 = num1 = 1;

回答by Sanjay Kumaar

It is simple.

很简单。

int num1,num2;
num1 = num2 = 5;