Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : assign data to properties via constructor vs. instantiating

Supposing I have an Album class :

public class Album  {     public string Name {get; set;}     public string Artist {get; set;}     public int Year {get; set;}      public Album()     { }      public Album(string name, string artist, int year)     {         this.Name = name;         this.Artist = artist;         this.Year = year;     } } 

When I want to assign data to an object of type Album, what is the difference between the next 2 approaches :

Via Constructor

var albumData = new Album("Albumius", "Artistus", 2013); 

or when instantiating

var albumData = new Album                      {                          Name = "Albumius",                          Artist = "Artistus",                          Year = 2013                     }; 
like image 450
VasileF Avatar asked Oct 02 '13 13:10

VasileF


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


2 Answers

Both approaches call a constructor, they just call different ones. This code:

var albumData = new Album                  {                      Name = "Albumius",                      Artist = "Artistus",                      Year = 2013                 }; 

is syntactic shorthand for this equivalent code:

var albumData = new Album(); albumData.Name = "Albumius"; albumData.Artist = "Artistus"; albumData.Year = 2013; 

The two are almost identical after compilation (close enough for nearly all intents and purposes). So if the parameterless constructor wasn't public:

public Album() { } 

then you wouldn't be able to use the object initializer at all anyway. So the main question isn't which to use when initializing the object, but which constructor(s) the object exposes in the first place. If the object exposes two constructors (like the one in your example), then one can assume that both ways are equally valid for constructing an object.

Sometimes objects don't expose parameterless constructors because they require certain values for construction. Though in cases like that you can still use the initializer syntax for other values. For example, suppose you have these constructors on your object:

private Album() { } public Album(string name) {     this.Name = name; } 

Since the parameterless constructor is private, you can't use that. But you can use the other one and still make use of the initializer syntax:

var albumData = new Album("Albumius")                 {                      Artist = "Artistus",                      Year = 2013                 }; 

The post-compilation result would then be identical to:

var albumData = new Album("Albumius"); albumData.Artist = "Artistus"; albumData.Year = 2013; 
like image 195
David Avatar answered Oct 16 '22 20:10

David


Object initializers are cool because they allow you to set up a class inline. The tradeoff is that your class cannot be immutable. Consider:

public class Album  {     // Note that we make the setter 'private'     public string Name { get; private set; }     public string Artist { get; private set; }     public int Year { get; private set; }      public Album(string name, string artist, int year)     {         this.Name = name;         this.Artist = artist;         this.Year = year;     } } 

If the class is defined this way, it means that there isn't really an easy way to modify the contents of the class after it has been constructed. Immutability has benefits. When something is immutable, it is MUCH easier to determine that it's correct. After all, if it can't be modified after construction, then there is no way for it to ever be 'wrong' (once you've determined that it's structure is correct). When you create anonymous classes, such as:

new {      Name = "Some Name",     Artist = "Some Artist",     Year = 1994 }; 

the compiler will automatically create an immutable class (that is, anonymous classes cannot be modified after construction), because immutability is just that useful. Most C++/Java style guides often encourage making members const(C++) or final (Java) for just this reason. Bigger applications are just much easier to verify when there are fewer moving parts.

That all being said, there are situations when you want to be able quickly modify the structure of your class. Let's say I have a tool that I want to set up:

public void Configure(ConfigurationSetup setup); 

and I have a class that has a number of members such as:

class ConfigurationSetup {     public String Name { get; set; }     public String Location { get; set; }     public Int32 Size { get; set; }     public DateTime Time { get; set; }      // ... and some other configuration stuff...  } 

Using object initializer syntax is useful when I want to configure some combination of properties, but not neccesarily all of them at once. For example if I just want to configure the Name and Location, I can just do:

ConfigurationSetup setup = new ConfigurationSetup {     Name = "Some Name",     Location = "San Jose" }; 

and this allows me to set up some combination without having to define a new constructor for every possibly permutation.

On the whole, I would argue that making your classes immutable will save you a great deal of development time in the long run, but having object initializer syntax makes setting up certain configuration permutations much easier.

like image 21
sircodesalot Avatar answered Oct 16 '22 22:10

sircodesalot