1

Suppose I have the following structure in C#

public struct User
{
    public string name;
    public int ID;
    public bool isAlive;
}

and I want to assign all the values for this structure. The only way I know how to do this is through

User Bill;
Bill.ID = 1;
Bill.name = "Bill";
Bill.isAlive = false;

But is there a faster way to do this? Something like

User Bill = {
  ID : 1,
  name : "Bill",
  isAlive : false
};
10
  • See stackoverflow.com/questions/740658/… Commented Oct 23, 2013 at 22:38
  • 1
    Mutable structs are evil. You should not use them. Commented Oct 23, 2013 at 22:41
  • 1
    @Servy - well, C# is evil, sou you should not use it. The same is true for PHP, Java, C and everything that is NOT either PHP, Java, C or C#. Must be true - I read each of these on the internet. Commented Oct 23, 2013 at 22:43
  • @SalvadorDali There are many, many situations in which having mutable structs allows for highly confusing code that is very hard to reason about, ranging from very common cases, often involving mutating a copy of a value type without realizing it, to more obscure cases. You really need to be very intimately familiar with the language and how it functions to really confidently use a mutable value type safely, and those who have such knowledge know enough to know that it's not worth the mental effort unless it's really needed. Commented Oct 23, 2013 at 22:46
  • 1
    Put another way, why is User a struct and not a class? There are plenty of good arguments as to why, based on what you've shown here, this should be a class, programmers.stackexchange.com/questions/92339/… for example. Commented Oct 23, 2013 at 22:57

5 Answers 5

3

Yes.

User bill = new User{
  ID = 1,
  name = "Bill",
  isAlive = false
};
Sign up to request clarification or add additional context in comments.

Comments

3

Make a constructor for your struct :

public struct User
{
    public string name;
    public int ID;
    public bool isAlive;

    public User(string name, int ID, bool isAlive)
    {
        this.name = name;
        this.ID = ID;
        this.isAlive = isAlive;
    }
}

Then use it for initialization :

User bill = new User("Bill", 1, false);

Comments

1

Structs behave exactly like classes (just that they are a value type), so you could - for instance - use constructors and methods.

1 Comment

Structs and classes are fundamentally different. The notion that they should be expected to work the same is responsible for some people's condemnation of mutable struct semantics, when it is the notion that everything should behave like a class object which is fundamentally wrong.
1

While @Somya's answer is correct and works, I tend not to use this construct. IMHO the correct way to do this is to create a constructor for User, that takes the needed parameters, then call new User (1, "Bill", false).

This could easily be considered a spleen.

Comments

0

Suppose you don't have that structure .. you can use anonymous type:

var Bill=new {
    ID=1,
    name="Bill",
    isAlive=false
};

var Gates=new {
    ID=2,
    name="Gates",
    isAlive=true
};

Note the remarks:

If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same compiler-generated type information.

Structures are immutable, but an instances of anonymous type is read-only once it's assigned:

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

So you gained both of the benefits with anonymous types.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.