Click here to Skip to main content
15,921,959 members
Home / Discussions / C#
   

C#

 
GeneralRe: Parentheses vs curly brackets Pin
User987432-Jan-18 23:13
professionalUser987432-Jan-18 23:13 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz28-Dec-17 7:08
mveGerry Schmitz28-Dec-17 7:08 
GeneralRe: Parentheses vs curly brackets Pin
User987432-Jan-18 22:29
professionalUser987432-Jan-18 22:29 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz3-Jan-18 5:13
mveGerry Schmitz3-Jan-18 5:13 
GeneralRe: Parentheses vs curly brackets Pin
Pete O'Hanlon26-Dec-17 22:26
mvePete O'Hanlon26-Dec-17 22:26 
AnswerRe: Parentheses vs curly brackets Pin
BillWoodruff26-Dec-17 15:31
professionalBillWoodruff26-Dec-17 15:31 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 18:10
professionalUser9874326-Dec-17 18:10 
AnswerRe: Parentheses vs curly brackets Pin
Nathan Minier27-Dec-17 6:32
professionalNathan Minier27-Dec-17 6:32 
Just to add some clarification, since there seems to be so much consternation.

One of those is an explicit constructor call, MyClass(). The second is an initializer, which allows you to populate values as you see fit regardless of their "safety". It also requires a default constructor for the class, otherwise you're stuck calling the constructor then the initializer. This all means that if a constructor is defined with arguments, you can initialize in a safe way as defined by whoever created that class. For instance:
C#
public class MyClass
{
   public string Greeting { get; set; }
   public bool IsGoodDayGreeting { get; set; }

   public MyClass(){} //default constructor i.e. no arguments

   public MyClass(string greeting)
   {
      Greeting = greeting;
      IsGoodDayGreeting = greeting.Equals("Good Morning") || greeting.Equals("Good Day"); //set flag
   }
}

Now that we've got the class, we have 3 ways to initialize it:
C#
var greeting = new MyClass("Good Day"); // new myclass with a true flag

// new myclass false flag, but should be true per constructor. 
var greeting3 = new MyClass { Greeting = "Good Morning" }; 

// new myclass which should be false per constructor, but is true
var greeting4 = new MyClass("Hello") { IsGoodDayGreeting = true };

As long as you bear the possible deviations in mind when breaking away from the constructor this is all fine and dandy, and a good dev will make sure that Object Initialization will not break an object's functionality, but if you rely on that without checking you are ultimately doomed to disappointment.

The last piece is constructor initialization. This only applies to items that implement IEnumerable, so it will work with Arrays, Lists, Dictionaries, etc.
C#
var greetings = new List<MyClass>
{
   new MyClass("Hello"),
   new MyClass("Good Morning"),
   greeting2,
   greeting4
};

As expected, that results in a list populated with 2 new instances of MyClas and, assuming it's in scope with the above code, the previously defined greeting2 and greeting4.

Anyway, I hope this is what you needed.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli

GeneralRe: Parentheses vs curly brackets Pin
User9874327-Dec-17 9:03
professionalUser9874327-Dec-17 9:03 
GeneralRe: Parentheses vs curly brackets Pin
Nathan Minier27-Dec-17 9:10
professionalNathan Minier27-Dec-17 9:10 
QuestionSerialize class with multiple different schemas Pin
hpjchobbes26-Dec-17 2:53
hpjchobbes26-Dec-17 2:53 
AnswerRe: Serialize class with multiple different schemas Pin
BillWoodruff26-Dec-17 5:25
professionalBillWoodruff26-Dec-17 5:25 
GeneralRe: Serialize class with multiple different schemas Pin
hpjchobbes27-Dec-17 3:20
hpjchobbes27-Dec-17 3:20 
GeneralRe: Serialize class with multiple different schemas Pin
Pete O'Hanlon27-Dec-17 4:05
mvePete O'Hanlon27-Dec-17 4:05 
AnswerRe: Serialize class with multiple different schemas Pin
jschell26-Dec-17 7:28
jschell26-Dec-17 7:28 
AnswerRe: Serialize class with multiple different schemas Pin
Gerry Schmitz26-Dec-17 12:49
mveGerry Schmitz26-Dec-17 12:49 
GeneralRe: Serialize class with multiple different schemas Pin
hpjchobbes27-Dec-17 3:26
hpjchobbes27-Dec-17 3:26 
GeneralRe: Serialize class with multiple different schemas Pin
Gerry Schmitz28-Dec-17 6:28
mveGerry Schmitz28-Dec-17 6:28 
QuestionType Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874325-Dec-17 23:09
professionalUser9874325-Dec-17 23:09 
AnswerRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
Gerry Schmitz26-Dec-17 12:28
mveGerry Schmitz26-Dec-17 12:28 
GeneralRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874326-Dec-17 12:52
professionalUser9874326-Dec-17 12:52 
GeneralRe: Type Casting from OleDb Value Types Returns Invalid Cast Exception Pin
User9874326-Dec-17 13:02
professionalUser9874326-Dec-17 13:02 
QuestionAwait/Async Question Pin
Kevin Marois24-Dec-17 10:51
professionalKevin Marois24-Dec-17 10:51 
AnswerRe: Await/Async Question Pin
Dave Kreskowiak24-Dec-17 15:15
mveDave Kreskowiak24-Dec-17 15:15 
GeneralRe: Await/Async Question Pin
Kevin Marois24-Dec-17 15:22
professionalKevin Marois24-Dec-17 15:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.