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

  • Home
  • SEARCH
  • 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 9093183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:01:40+00:00 2026-06-16T23:01:40+00:00

Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? Most of

  • 0

Possible Duplicate:
Best Practice: Initialize class fields in constructor or at declaration?

Most of the time, I see the way of initializing a variable like this

public class Test
{
    private int myIntToInitalize;

    public Test()
    {
        myIntToInitalize = 10;
    }
}

From my perspective, this is the most general way of initializing a variable. Most of the code in books, blogs and also internal implementations of .NET are equivalent to my example.

Recently I saw people doing the initialization directly, so without setting the value in the constructor.

public class Test
{
    private int myIntToInitalize = 10;
}

In point of view, there is not difference whether initialize and declare a variable or initialize the variable in the constructor.

Apart from the best practices and the length of code lines, where are the benefits of initialize a variable directly and are there subtle differences?

  • 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-16T23:01:42+00:00Added an answer on June 16, 2026 at 11:01 pm

    There’s one potentially significant difference in some cases.

    Instance initializers are executed before the base class constructor is executed. So if the base class constructor invokes any virtual methods which are overridden in the derived class, that method will see the difference. Usually this shouldn’t be a noticeable difference, however – as invoking virtual methods in a constructor is almost always a bad idea.

    In terms of clarity, if you initialize the variable at the point of declaration, it makes it very clear that the value doesn’t depend on any constructor parameters. On the other hand, keeping all the initialization together helps readability too, IMO. I would try to make sure that wherever possible, if you have multiple constructors they all delegate to one “master” constructor which does all the “real” initialization – which means you’ll only put those assignments in one place either way.

    Sample code to demonstrate the difference:

    using System;
    
    class Base
    {
        public Base()
        {
            Console.WriteLine(ToString());
        }
    }
    
    class Derived : Base
    {
        private int x = 5;
        private int y;
    
        public Derived()
        {
            y = 5;
        }
    
        public override string ToString()
        {
            return string.Format("x={0}, y={1}", x, y);
        }
    }
    
    class Test
    {
        static void Main()
        {
            // Prints x=5, y=0
            new Derived();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? this is
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Possible Duplicate: C# member variable initialization; best practice? Which is the right way to
Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
Possible Duplicate: Why use partial classes? Which way is best practice and the best
Possible Duplicate: Loading multiple versions of the same class What is the best way
Possible Duplicate: Initializing in constructors, best practice? Advantages of using initializer list? I have
Possible Duplicate: Initializing in constructors, best practice? I'm new to C++. Suppose we have
Possible Duplicate: Best practice: Import mySQL file in PHP; split queries How to import
Possible Duplicate: Passing only two variables between controller and view - best practice? There

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.