properties - C#: How to set default value for a property in a partial class? -


I am very new to C # so that please stand with me ...

I apply I am doing a partial class, and like wanting to add two properties:

  public partial class SomeModel {public bool isSomething {get} Set; } Public listing & lt; String & gt; Get some listings { Set; } ... Additional methods for using the above Data Members ...}  

I would like to introduce both data members: IsSomething to True and SomeList to new list & lt; String & gt; () . Generally I will do it in the constructor, however, because it is a partial square, I do not want to touch the constructor (am I?).

What is the best way to achieve this?

Thanks

PS I am working in ASP.NET MVC, adding functionality to some models, so partial orbit.

updated for C # 6

The ability to assign a default value to C # 6 auto-properties added value can be any expression (it does not have to be a constant). Here are some examples:

  // string literally start with public string SomeProperty {get; Set;} = "This is the default value"; // Start with a simple expression Public Date Time Created {fill;} = DateTime.Now; // Start with a conditional expression {Receive Public Bull ISFu; } = Some class. Some properties? right wrong;   Original answer  

Automatically implemented properties can be started in class constructor, but not on proprietors themselves.

Some models in the public {ISSomething = false; SomeList = new list & lt; String & gt; (); }

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

  private bool_Isthing = false; What is Public Bull (Meet {return _ ISS some;} set {_IsSomething = value;}}  

Update: My answer above is in partial class Does not explain the issue. Provides a solution to use partial method, which is in line with my first advice. My second suggestion is to use non-automatically executed properties (manually implemented properties) Working for the situation


Comments

Popular posts from this blog

graphviz - size of node with shape=circle -

android - TCP sending with Unity -