views:

987

answers:

5

Hello,

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.

+2  A: 

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:

private bool isSomething = true;
public bool IsSomething {
    get { return this.isSomething; }
    set { this.isSomething = value; }
}

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.

Reed Copsey
+1 for pointing out the different behaviors of value/reference types.
STW
+7  A: 

Automatically implemented properties can be initialized in the class constructor, but not on the propery itself.

public SomeModel
{
    IsSomething = false;
    SomeList = new List<string>();
}

...or you can use a field-backed property (slightly more work) and initialize the field itself...

private bool _IsSomething = false;
public bool IsSomething
{
    get { return _IsSomething; }
    set { _IsSomething = value; }
}

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.

STW
SomeList = new List<string>**()**;
grenade
Regarding your first suggestion, is it considered "good practice" to do so? It will certainly be easier, but feels like "code smell"...
Roee Adler
@RaxL I'm on the fence about the smell of it. Initialization logic is a fairly normal part of OO development; provided that the developers working on the system follow the same practices then I don't htink it would be an issue.
STW
@Yoooder: Many thanks!
Roee Adler
A: 

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.

Programming Hero
As Reed pointed out in his comment the value-types will initialize with a default value (0 for int, false for bool...) but the reference types will initialize to null/Nothing. So in his case IsSomething = false and SomeList = null
STW
@Yooder: this answer was provided when the question still asked for defaults of False and null. Please retract your -1, it's unfair for @Programming Hero
Roee Adler
Moving the goal posts mid-game? No fair!
Programming Hero
+4  A: 

You can't have two constructors in two parts of a partial class. However, you can use partial methods to accomplish something like it:

// file1:
partial void Initialize();
public Constructor() {
    // ... stuff ... initialize part 1
    Initialize();
}

// file2:
void Initalize() {
    // ... further initializations part 2 might want to do
}

If no parts of a partial class defines the partial method, all calls to it would be omitted.

Mehrdad Afshari
+1  A: 

To this, don't use automatic property but the old way

YourType _yourParameter = yourDefaultValue;
public YourType YourParameter
{
   get{return _yourParameter;}
   set{_yourParameter=value;}
}
Gregoire