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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:49:12+00:00 2026-05-13T05:49:12+00:00

Are there any differences between the following two ways of field initialization? When to

  • 0

Are there any differences between the following two ways of field initialization? When to use which one?

First way

public class Class1
{
   private SomeClass someclass;

   public Class1()
   {
       someclass = new SomeClass(some arg);
   }
}

Second way

public class Class1
{
   private SomeClass someclass = new SomeClass(some arg);
}

The field in the second example could be readonly.

  • 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-13T05:49:12+00:00Added an answer on May 13, 2026 at 5:49 am

    You cannot make use of the this keyword when initializing fields inline. The reason for this is the order in which the code is executed: for all intents and purposes, the code to initialize a field inline is run before the constructor for the class (i.e. the C# compiler will prevent access to the this keyword). Basically what that means is this will not compile:

    public class Class1
    {
       private SomeClass someclass = new SomeClass(this);
    
       public Class1()
       {
       }
    }
    

    but this will:

    public class Class1
    {
       private SomeClass someclass;
    
       public Class1()
       {
           someclass = new SomeClass(this);
       }
    }
    

    It’s a subtle difference, but one worth making a note of.

    The other differences between the two versions are only really noticeable when using inheritance. If you have two classes which inherit from each other, the fields in the derived class will be initialized first, then the fields in the base class will be initialized, then the constructor for the base class will be invoked, and finally, the constructor for the derived class will be invoked. There are some cases where you need to be very careful with this, as it could cause a fruit salad of complications if you don’t realise what is going on (one of which involves calling a virtual method inside the base class constructor, but that is almost never a wise move). Heres an example:

    class BaseClass
    {
        private readonly object objectA = new object(); // Second
        private readonly object objectB;
    
        public BaseClass()
        {
            this.objectB = new object(); // Third
        }
    }
    
    class DerivedClass : BaseClass
    {
        private object objectC = new object(); // First
        private object objectD;
    
        public DerivedClass()
        {
            this.objectD = new object(); // Forth
        }
    }
    

    You will need to set breakpoints on all of the lines that initialize fields to be able to see the correct sequence.

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

Sidebar

Related Questions

Are there any differences between the following ways of initialization of variables? @var ||=
Is there any difference between following two ways of creating an object. Student s1
Are there any differences between following two indexes? IDX_IndexTables_1 IDX_IndexTables_2 If there are any,
In VB.Net is there any difference between the following three ways of initialising object
Is there any performance difference between the following two statements? from item in collection
Is there any difference between moving the mouse in windows using the following two
Is there any performance difference between the two following queries: SELECT foo,bar FROM table
Are there any differences between the original CruiseControl and the .NET port? I've compared
So I was wondering if there are any major differences between the various implementations
Are there any major differences in performance between http and https? I seem to

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.