I need to give my properties initial values, and I'd like to know if there is a simpler way than using a constructor.
-
im curious as to why you think a ctor is not "simple"? – RPM1984 Nov 15 '10 at 06:13
-
var myClass = new MyClass {Property1 = value1, Property2 = value2}; – Saeed Amiri Nov 15 '10 at 06:17
5 Answers
Are you using automatically implemented properties? The value type properties will be initialized to their default values. Reference types will be initialized to null.
If you want them to initialize them to some other I think the best option is to set them in the constructor.
If you are not using automatically implemented properties you can initialize them where they are declared.
It will also be useful to keep in mind the order in which the objects fields and constructors are initialized.

- 9,256
- 4
- 38
- 51
If you are taking advantage of automatic properties, the constructor is the easiest way. If you are not, then your member variable can define the default value.
private string _someString = "Hello.";
But, you'll have to define the getter and setter yourself.
public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
Which wouldn't be simpler than just defining the default in the constructor.

- 1,071
- 8
- 12
If you just want to initialize the property values, then constructors are simply enough I don't see why you think they are not simple.
However you can do this if you want, you can initialize the variable like this :
int _myProperty = 5;
public int MyProperty
{
get{ return _myProperty ;}
set { _myProperty=value; }
}

- 2,141
- 7
- 29
- 39
I like to think about this a bit differently. If the value is something that is mandatory for the creation of an instance, it needs to be a parameter for the constructor.
However, if the value is optional then setting it via a property is fine.

- 114,645
- 34
- 221
- 317
Well Constructors are best for doing this. How ever u can call a methods too.
Check following link too Initializing C# auto-properties
-
-
No, they are not. They may be the most efficient way in terms of CPU speed, but in terms of maintenance -- in case of >1 arguments -- it is better to use in-place assignment, especially if no special logic is involved. Example: new bar() { x = 5, y = 7 }; This forces user of class bar to name all properties/fields, while using constructor with arguments just allows using names (not mentioning, the latter method is possible only with Net 4.0). – greenoldman Nov 15 '10 at 07:19
-
@Giffyguy Check "this works perfectly" again. Per MSDN: "Warning: A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code." on [DefaultValueAttribute](http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx) -- as I learned the hard way, it is prudent to heed this warning. This "default value" is only accessible via. reflection -- *it is not actually assigned* (this attribute is used for DataContractSerializer, etc where not saving the default value can save space). – Dec 11 '10 at 08:55