0

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
};
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • See http://stackoverflow.com/questions/740658/whats-the-difference-between-an-object-initializer-and-a-constructor – Chris Laplante Oct 23 '13 at 22:38
  • 1
    Mutable structs are evil. You should not use them. – Servy Oct 23 '13 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. – Eugen Rieck Oct 23 '13 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. – Servy Oct 23 '13 at 22:46
  • @SalvadorDali Your type also doesn't logically represent a single value, is not a particularly small size, isn't immutable, etc. so it really shouldn't be a value type from a conceptual point of view. – Servy Oct 23 '13 at 22:47
  • @EugenRieck That is correct. You should use none of those things. It's machine code or bust. – Servy Oct 23 '13 at 22:48
  • 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, http://programmers.stackexchange.com/questions/92339/when-do-you-use-a-struct-instead-of-a-class for example. – Preston Guillot Oct 23 '13 at 22:57
  • I guess the word *faster* in this context means simpler and less statements. – Ken Kin Oct 23 '13 at 23:30
  • The fundamental question that should be asked in selecting between a struct and a class is whether the data type is supposed to represent a user, or whether it's supposed to represent three values stuck together with duct tape. If the former, it should be a mutable class. If the latter, it should be an immutable class or an exposed-field structure. – supercat Oct 24 '13 at 04:58
  • @Servy: If instances of a data type shouldn't have a persistent identity, the fact that it *doesn't* represent a single unified entity [e.g. a `PointF` represents two `float` values which, though related, are independent] is an argument *in favor* of using an exposed-field struct. I'm not saying the poster's proposed struct is a good one, but mutable class objects should be avoided for things that shouldn't have a composite identity, and immutable types avoided for things which may need to be partially modified. – supercat Oct 24 '13 at 05:09

5 Answers5

3

Yes.

User bill = new User{
  ID = 1,
  name = "Bill",
  isAlive = false
};
NomadTraveler
  • 1,086
  • 1
  • 12
  • 37
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);
Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
1

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

Aurelia
  • 1,052
  • 6
  • 28
  • 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. – supercat Oct 24 '13 at 04:56
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.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
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.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76