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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:41:23+00:00 2026-05-27T02:41:23+00:00

I have code like this: public class test { public string aa { get;

  • 0

I have code like this:

public class test {
    public string aa { get; set; }
    public string bb { get; set; }
    public string cc { get; set; }
}

var a = new test {
    aa = "a",
    bb = "b"
}

var d = a;
d.cc = "c";

Is there any way for me to create class d and at the same time populate it. So for example the last two lines could be combined into one? Note that I want to avoid doing this with a constructor as sometime it will be different fields other than cc which I will populate.

please note my edit

var d = a;
  • 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-27T02:41:23+00:00Added an answer on May 27, 2026 at 2:41 am

    Given your new description of the problem, how about this:

    Note that it changes a as well (same as your code), it doesn’t create a copy. If you want a copy, go with Oded’s solution.

    public class test
    {
        public string aa { get; set; }
        public string bb { get; set; }
        public string cc { get; set; }
    
        public test AlterTest(Action<test> alteration)
        {
            alteration(this);
            return this;
        }
    }
    
    static void Main(string[] args)
    {
        var a = new test
        {
            aa = "a",
            bb = "b"
        };
    
        var d = a.AlterTest((t) => t.cc = "c");
    }
    

    I’d say this isn’t as readable as your original code though (as you need to know what AlterTest does, which isn’t a lot).

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

Sidebar

Related Questions

i have code like this public class People { public string name { get;
I have code that looks like this: template<class T> class list { public: class
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
Say, I have a code snippet like this: public static void main(String[] args) {
I have some code like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Save([Bind(Prefix=)]Person person) { String s
I have code like this: var newMsg = new Msg { Var1 = var1,
Say that I have a class like: public class Test { public Func<string, bool>
I have an enum whose code is like this - public enum COSOptionType {
In our application, I have seen code written like this: User.java (User entity) public
We have several areas where code like this can be seen public Map extractData(ResultSet

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.