Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 319221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:38:29+00:00 2026-05-12T08:38:29+00:00

I’m very new to C# so please bear with me… I’m implementing a partial

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:38:30+00:00Added an answer on May 12, 2026 at 8:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are only fecthing a single row, i would… May 13, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer Trigger the form's submit() event when the field loses focus.… May 13, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer Sounds like overkill to use a Windows Service to send… May 13, 2026 at 2:04 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.