C# – using object initializers

Today something about simplifying your life.

 

While programming, there are so many situations when you want to write a simplier code. Maybe not in a „short code” project, but when you develop applications of 5000 – 6000 and more, you realize that you should write as best code as it is possible.

Since July, I have been working on a big project with other developers. There are so many situations in which I can’t read the code after two weeks, both if it is my code or other developer. In this post I want you to focus on object initializers.

 

Object initialization:

 

1. Using object initializer:

2. Using constructor to initialize:

 

 

3. Using accessors:

As you can see -> in the last example, class CSettings has a lot lines of code, and in my opinion it is the worst implementation. Of course sometimes you can’t use only the first option, but I wanted to show you how to write the best code in this situation.

 

MJ

3 uwagi do wpisu “C# – using object initializers

  1. Implementation of CSettings is „normal” 🙂
    Don’t worry about size. If you however want to make it „simplier” you can use auto properties
    public string Name {get;set;}

Zostaw komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Connecting to %s