24

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.

Roee Adler
  • 33,434
  • 32
  • 105
  • 133

8 Answers8

41

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:

// Initialize to a string literal
public string SomeProperty {get;set;} = "This is the default value";

// Initialize with a simple expression
public DateTime ConstructedAt {get;} = DateTime.Now;

// Initialize with a conditional expression
public bool IsFoo { get; } = SomeClass.SomeProperty ? true : false;

Original Answer

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.

Community
  • 1
  • 1
STW
  • 44,917
  • 17
  • 105
  • 161
  • SomeList = new List**()**; – grenade Aug 13 '09 at 15:19
  • Regarding your first suggestion, is it considered "good practice" to do so? It will certainly be easier, but feels like "code smell"... – Roee Adler Aug 13 '09 at 15:20
  • @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 Aug 13 '09 at 15:24
  • Note this, adding any code (including this answer) in your auto generated class will wiped away after a refresh of your model. – H35am Aug 24 '17 at 21:40
  • @H35am you should never modify generated classes (scaffolding is a different story--you never "refresh" or re-generate scaffolding) – STW Aug 24 '17 at 23:28
  • But how do you implement your answer in a partial class? Since the problem states that some functionality is needed in the Model class that is auto generated. – H35am Aug 24 '17 at 23:41
9

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
  • 554,122
  • 78
  • 1,158
  • 1,373
7

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
  • 414,610
  • 91
  • 852
  • 789
  • 1
    Alas, this does not help if the part of the class that has the constructor is generated code. – Vaccano Aug 23 '13 at 00:08
5

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 :

    private bool _sendEmail = true;

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.

public partial class EndOfDayPackageInfo
{
    [OnDeserialized()]
    public void Init(StreamingContext context)
    {
        _sendEmail = true;
    }

    private bool _sendEmail;
    public bool SendEmail
    {
        get
        {
            return _sendEmail;
        }
        set
        {
            _sendEmail = value;
            RaisePropertyChanged("SendEmail");
        }
    }

}

Another approach is to 'lazy load' the property - but this approach is much less elegant.

    private bool _sendEmail;
    private bool _sendEmailInitialized;

    public bool SendEmail
    {
        get
        {
            if (!_sendEmailInitialized)
            {
                _sendEmailInitialized = true;
                _sendEmail = true;  // default value
            }

            return _sendEmail;
        }
        set
        {
            if (!_sendEmailInitialized)
            {
                // prevent unwanted initialization if 'set' is called before 'get'
                _sendEmailInitialized = true;
            }

            _sendEmail = value;
            RaisePropertyChanged("SendEmail");
        }
    }
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • How interesting. Makes sense in a twisted way. – NotMe Dec 01 '10 at 03:49
  • ya but very confusing at first. just discovered this today and thought it was a WPF binding issue until I finally realized it was WCF. turns out they call this method which circumvents calling any constructor : http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getuninitializedobject.aspx – Simon_Weaver Dec 01 '10 at 04:40
  • what does RaisePropertyChange method? does this method is from .net framework?? – ncubica Nov 29 '12 at 15:46
4

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

YourType _yourParameter = yourDefaultValue;
public YourType YourParameter
{
   get{return _yourParameter;}
   set{_yourParameter=value;}
}
Gregoire
  • 24,219
  • 6
  • 46
  • 73
3

For user of version 6.0 of C#, it's possible to initialize the properties like this :

public bool IsSomething { get; set; } = true;
public List<string> SomeList { get; set; } = new List<string>();
Ant
  • 277
  • 3
  • 10
1

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.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • 1
    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 Aug 13 '09 at 15:26
  • @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 Aug 13 '09 at 15:38
  • Moving the goal posts mid-game? No fair! – Paul Turner Aug 14 '09 at 09:07
0
 private bool _InternalUserContactUpdate = false;
        public bool InternalUserContactUpdate
        {
            get { return _InternalUserContactUpdate; }
            set { _InternalUserContactUpdate = value; }
        }

Then when you want to override the value on condition,

if(!objUserModel.FirstName.ToLower().Equals(entry.Key[0].Attributes.Contains("firstname").ToString().ToLower()))
        {
             objUserModel.InternalUserContactUpdate= true;
        }

Hope this will help

Subhamay
  • 197
  • 1
  • 5