Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do braces after C# new statement do?

Given the code below, what is the difference between the way position0 is initialized and the way position1 is initialized? Are they equivalent? If not, what is the difference?

class Program
{
    static void Main(string[] args)
    {
        Position position0 = new Position() { x=3, y=4 };

        Position position1 = new Position();
        position1.x = 3;
        position1.y = 4;
    }
}

struct Position
{
    public int x, y;
}
like image 682
Vivian River Avatar asked Oct 27 '10 15:10

Vivian River


People also ask

What is {} called in programming?

What Does Bracket Mean? Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts.

What do brackets do in C++?

Beginner programmers, and programmers coming to C++ from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.

Why do braces have curly C?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What are () these called?

Parentheses are a pair of punctuation marks that are most often used to add additional nonessential information or an aside to a sentence. Parentheses resemble two curved vertical lines: ( ). A single one of these punctuation marks is called a parenthesis.


3 Answers

They are not quite equivalent - at least not in the general case. The code using an object initializer is closer to this:

Position tmp = new Position();
tmp.x = 3;
tmp.y = 4;
Position position1 = tmp;

In other words, the assignment to the variable only occurs after the properties have been set. Now in the case where you're declaring a new local variable, that doesn't actually matter, and the compiler may well optimize to your first form. But logically, it does matter. Consider:

Position p1 = new Position { x = 10, y = 20 };

p1 = new Position { x = p1.y, y = p1.x };

If that did the assignment to p1 first, you'd end up with 0 for both p1.x and p1.y. Whereas that's actually equivalent to:

Position tmp = new Position();
tmp.x = 10;
tmp.y = 20;
Position p1 = tmp;

tmp = new Position();
tmp.x = p1.y; // 20
tmp.y = p1.x; // 10
p1 = tmp;

EDIT: I've just realised that you're using a struct rather than a class. That may make some subtle differences... but you almost certainly shouldn't be using a mutable struct to start with :)

like image 141
Jon Skeet Avatar answered Oct 24 '22 11:10

Jon Skeet


Object and collection initializers, used to initialize fields on an object.

http://msdn.microsoft.com/en-us/library/bb384062.aspx

They produce nearly equivalent IL. Jon Skeet has the answer on what is really going on.

like image 49
Joshua Rodgers Avatar answered Oct 24 '22 11:10

Joshua Rodgers


That is an object initialiser, and simply allows you to assign values in a single expression. Most importantly, this also works inside LINQ an for anonymous types (otherwise immutable). There is also a similar collection initialiser syntax for addi items to new collections.

Note that there is a subtle timing issue that can be useful; with initialisers the assignments/adds all happen before the variable is assigned, which can help stop other threads seeing an incomplete object. You would otherwise need an additional variable to achieve the same result.

like image 7
Marc Gravell Avatar answered Oct 24 '22 11:10

Marc Gravell