212

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?

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!

10 Answers 10

342

It's as simple as:

num1 = num2 = 5;

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

Take for example a class that write to the console everytime the get and set accessor are invoked.

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.

13
  • 48
    Its not weird actually. The logic is as follows: The assignment operation itself returns a value, which is the value that has been assigned. The sequence of execution is: num1 = (num2 = 5) and the first assignment which is executed (num2 = 5) returns the value 5 to the outside world - which is in turn assigned to num1. This works ad infinitum (num0 = num1 = num2 = 5).
    – Jpsy
    Commented Jan 21, 2012 at 19:36
  • If you want to test it, try Console.WriteLine(num = 5);. (Disclaimer: I haven't yet) Commented May 9, 2013 at 19:36
  • 5
    Question: Is this considered bad practice num1 = num2 = 5? Does it turn code unreadable if more variables are involved? Sorry to hijack, I didn't want to create a new post just for it. Commented Dec 3, 2013 at 15:28
  • There is no correct answer. I use it only scarcely in my program and I could have avoided it. Commented Dec 3, 2013 at 18:21
  • Down to coding style I assume then. Thanks for the reply. Commented Dec 3, 2013 at 19:10
72

This will do want you want:

int num1, num2;
num1 = num2 = 5;

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

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.

0
41
int num1 = 5, num2 = 5;

Declaring and assigning variables in the same statement.

2
  • 2
    Is this a single statement or two statements? Commented Dec 26, 2017 at 18:41
  • great answer, but looks weird when "5" is a long string constant. string a = "really long string", b = "really long string"; Commented Mar 28, 2018 at 9:12
37

This is now a thing in C#:

var (a, b, c) = (1, 2, 3);

By doing the above, you have basically declared three variables. a = 1, b = 2 and c = 3. All in a single line.

4
  • 3
    Woah, didn't know this. Can you provide a link to documentation?
    – Dave
    Commented Oct 12, 2020 at 19:58
  • 5
    learn.microsoft.com/en-us/dotnet/csharp/deconstruct "Starting with C# 7.0, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single deconstruct operation. When you deconstruct a tuple, you assign its elements to individual variables. When you deconstruct an object, you assign selected values to individual variables."
    – Rikki
    Commented Oct 13, 2020 at 20:17
  • 2
    A colleague of mine showed some code that used this syntax in a demo this morning. I've been writing C# since 2003, but this was new to me. Thanks for your comment, and the link to the documentation. Commented Oct 8, 2021 at 21:05
  • 2
    Don't forget that tuple members can have different types, so this works, too: var (d, i, s) = (6.28, 42, "Hello"); Commented Mar 14, 2023 at 14:24
36

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

int num1, num2 = num1 = 1;
2
  • 4
    I think this is the best answer because both initialization and assignment can be done in a single statement. Commented Feb 25, 2020 at 11:30
  • Something to keep in mind is this only initialises values that are followed by = so it doesn't scale nicely. If you leave/forget any assignments, that value will be uninitialised so you have to do decimal a, b, c, d = a = b = c = 0m; compared to say decimal a = 0m, b = 0m, c = 0m, d = 0m;
    – laurencee
    Commented Aug 12 at 11:12
11

Try this:

num1 = num2 = 5;

Note that this won't work in VB.

3
  • 1
    Do you know if there is some alternate syntax for VB developers, or is multiple assignment simply not available in VB?
    – ckittel
    Commented Jun 25, 2011 at 20:50
  • 3
    @cki: AFAIK, it's not supported.
    – SLaks
    Commented Jun 26, 2011 at 2:33
  • @ckittel In VB = is used both for assignment and equality, so num1 = num2 = 5 would mean "assign the boolean result of whether or not five is equal to num2 to the variable num1". Because the = operator has these two meanings, there is no way to represent this syntax. The best you could do is create a function that took in a number of parameters byref as well as a value and assigned all of the parameters to that value.
    – Servy
    Commented Feb 14, 2013 at 19:35
7

num1 = num2 = 5

6
int num1, num2, num3;

num1 = num2 = num3 = 5;

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

Your example would be:

int num1 = 1;
int num2 = 1;

num1 = num2 = 5;
5

Something like this.

num1 = num2 = 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.