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 8905459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:57+00:00 2026-06-15T02:23:57+00:00

In C# you have object initializers to initialize fields of objects at creation time

  • 0

In C# you have object initializers to initialize fields of objects at creation time without using a constructor.

Now I’m wondering if there is an equivalent to classes which means that you can ‘initialize’ properties of classes when defining subclasses without actually using an override syntax but simply declaring what the value of a known property is.

Example:

public abstract class Car {
    public abstract string Name { get; }
}
// usual approach
public class Mustang : Car {
    public overwrite string Name { get { return "Ford Mustang"; } }
}
// my idea of avoiding boilerplate code
public class Mustang : Car { Name = "Ford Mustang" }

Is there a way to accomplish this? If there is none, could T4 templates be of any help?

  • 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-06-15T02:23:59+00:00Added an answer on June 15, 2026 at 2:23 am

    To make rekire’s example clearer, you’d write something like:

    public abstract class Car
    {
        public string Name { get; private set; }
    
        protected Car(string name)
        {
            this.Name = name;
        }
    }
    
    public class Mustang : Car
    {
        public Mustang() : base("Mustang")
        {
        }
    }
    

    EDIT: Another option is to use attributes, where you’d write:

    [CarName("Mustang")]
    public class Mustang : Car
    

    … having written appropriate reflection code in Car. I would strongly recommend that you don’t though. Of course, attributes may be useful in your real context.

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

Sidebar

Related Questions

I have an object now: class Items attr_accessor :item_id, :name, :description, :rating def initialize(options
I have an object, called Account, and there is a campaign source associated with
I have an object Document with nested Properties (Name, Value) collection. Now I want
Say I have a method signature like this: protected override void Initialize(params object[] parameters)
I aware that Java object constructors implicitly initialize their instance's non-static fields. However, I'm
I have Chrome 7.0 and am trying to access fields of a global object.
I have defined a custom object with multiple fields. For example say I have
For example, I have object user stored in database (Redis) It has several fields:
I have a Document object initialized in the init() method of the servlet and
I have several Entity Framework Code First DbContext objects that use a custom Initializer.

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.