I'm very new to C# so please bear with me...
I'm implementing a partial class, and would like to add two properties like so:
public partial class SomeModel
{
public bool IsSomething { get; set; }
public List<string> SomeList { get; set; }
... Additional methods using the above data members ...
}
I would like to initialize both data members: IsSomething
to True
and SomeList
to new List<string>()
. Normally I would do it in a constructor, however because it's a partial class I don't want to touch the constructor (should I?).
What's the best way to achieve this?
Thanks
PS I'm working in ASP.NET MVC, adding functionality to a a certain model, hence the partial class.
The first property (IsSomething) is a boolean. It will be false by default.
The second property, since it's a reference type, will default to null without any effort on your part. You don't need to touch the constructor, since reference types (classes) will automatically start off as null in .NET.
If you wanted to use a non-default value, you'd have two options -
First, use a backing storage field:
Second option - add it to the constructor.
Note that the first option has no extra overhead - it's basically what the compiler does when you use an automatic property.
Then when you want to override the value on condition,
Hope this will help
Both your properties will already have the default values you require.
There is nothing wrong with having a constructor in a partial class. Partial classes are in no way special, aside from the fact that their source code is spread across multiple files/declarations.
WARNING for users of WCF partial classes
If you're trying to add a property to a WCF proxy class (generated by Add Service Reference) you might be surprised to find that private fields aren't initialized because apparently no constructor at all is called.
If you attempt to do this (as suggested in some other answers) it won't ever get called :
This has nothing to do with whether the field is in a partial class or not.
What you have to do is add an [OnDeserialized] attribute which lets you do further initialization to the object. This is part of System.Runtime.Serialization so is only useful in the context of serialization when using DataContractSerializer.
}
Another approach is to 'lazy load' the property - but this approach is much less elegant.
Updated for C# 6
C# 6 has added the ability to assign a default value to auto-properties. The value can be any expression (it doesn't have to be a constant). Here's a few examples:
Original Answer
Automatically implemented properties can be initialized in the class constructor, but not on the propery itself.
...or you can use a field-backed property (slightly more work) and initialize the field itself...
Update: My above answer doesn't clarify the issue of this being in a partial class. Mehrdad's answer offers the solution of using a partial method, which is in line with my first suggestion. My second suggestion of using non-automatically implemented properties (manually implemented properties?) will work for this situation.
To this, don't use automatic property but the old way