Questions tagged [object-initializer]

13 questions
200
votes
8 answers

What's the difference between an object initializer and a constructor?

What are the differences between the two and when would you use an "object initializer" over a "constructor" and vice-versa? I'm working with C#, if that matters. Also, is the object initializer method specific to C# or .NET?
Pete
  • 10,651
  • 9
  • 52
  • 74
17
votes
6 answers

How can I use collection initializer syntax with ExpandoObject?

I've noticed that the new ExpandoObject implements IDictionary which has the requisite IEnumerable> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to…
10
votes
4 answers

Should I use an object initializer or a constructor?

I have just learned about object initializers and was wondering what the best practices for when to use them are. This is what I read about them: http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx It makes it clear that they are necessary…
Sarien
  • 6,647
  • 6
  • 35
  • 55
4
votes
3 answers

how to add an item to a object initialized with { blah = "asdf" }

how to add an item to an object initialized with: object obj = new { blah = "asdf" }; If I want to add another key value pair, how would i?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
4
votes
1 answer

How can I remove all gridlines on an Excel spreadsheet except those explicitly added by me after clearing them all?

Yes, this question has been asked before, but the answer provided does not work for me. To be more specific: Wanting to only have gridlines where there is data (which I add myself), and remove the lighter gridlines that appear by default everywhere…
4
votes
1 answer

Object Initializer Formatting

Which setting(s) in Resharper 8 are reformatting my object initializer like this? (i.e. with the comma on it's own line). var snowDepthProcessor = new DataProcessor { Name = "Snow Depth" , DataRetriever =…
David Hayes
  • 7,402
  • 14
  • 50
  • 62
2
votes
0 answers

Object initializers in C++/CLI

In C# I can do this: foo( new SomeClass() { SomeProperty = "value" } ); Now in C++/CLI I can equivalently do this: auto tmp = gcnew SomeClass(); tmp->SomeProperty="value"; foo (tmp); But is there a syntax for C++/CLI that is similar to C# object…
Kevin Holt
  • 775
  • 9
  • 15
2
votes
1 answer

Linking command line argument parsing with object initialization

I have a class which has around a dozen object variables. Along with each variable, I want to provide a default value, a help string (for argparse) and a comment string (to write to a data file). I want to be able to instantiate objects…
user3443148
  • 411
  • 1
  • 4
  • 5
2
votes
1 answer

Spacing with ReSharper and the Object Initializer

When I try to initialize an object using the Object Initializer, ReSharper always inserts a lot of whitespace. public class Bar { public void Adam() { var foo = new Foo { First =…
Myrtle
  • 5,761
  • 33
  • 47
1
vote
2 answers

Creating an array of objects using initializers seems to fail

I have a GameObject we'll call the GM. Attached to it is a script that's meant to be the primary logical controller for a game. In that script, somewhere, I have: private dbEquipment equipment_database = new dbEquipment(); The relevant snippet from…
user462879
  • 187
  • 1
  • 13
0
votes
3 answers

Simple initilizators in method parameters

I built a fluent interface like this: criador.Include("Idade", 21).Include("Idade", 21); Can I do something like this: criador.Include({"Idade", 21},{"Idade", 21}); I tryed to user method with params keyword: public myType Include(params[]…
Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40
0
votes
2 answers

Visual Studio 2015 missing new keyword - no compiler error

I was working on one of our new applications when I made a stupid mistake… I forgot to write: ”new ClassName” inside an object initializer. Strangely VS just compiled the code… without any error or warning. (I’ve used VS 2015, .Net 4.5.2) My…
musium
  • 2,942
  • 3
  • 34
  • 67
-1
votes
1 answer

Visual Studio 2013 crashes consistently when typing in an object/collection initiazlier?

Has anyone else ever seen this happen? I've had Visual Studio crash over and over again when typing in an object initializer: https://msdn.microsoft.com/en-us/library/bb384062.aspx I suspect it's some kind of Intellisense bug, since it seems to…
Triynko
  • 18,766
  • 21
  • 107
  • 173