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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:24:32+00:00 2026-06-17T02:24:32+00:00

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

  • 0

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

this is just a simple and even a little stupid doubt that me and my friends raised while talking about our current project.

What I wanna know is the difference between calling a method to set a variable inside and outside the constructor of a class using C#, like in the example below:

Case 1:

public class Test
{
    string myVar = GetValue();

    public Test()
    {
    }
}

Case 2:

public class Test
{
    string myVar;

    public Test()
    {
        myVar = GetValue(); 
    }
}

Are there performance differences or any “pattern violations” when using any of these approaches? I would really appreciate if anyone could tell me which of these approaches is better and what really happens when I use them on a compiler level.

Thanks in advance!

  • 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-17T02:24:34+00:00Added an answer on June 17, 2026 at 2:24 am

    AFAIK the only difference is the field initialization (your first example) is triggered first – It’s still essentially treated as constructor code. There are no performance gains by choosing one over the other it’s really a matter of preference.

    One thing to be aware of is execution order as it can change based on your class hierarchy.

    • 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? Most of
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
Possible Duplicate: Caching $(this) in jQuery is a best practice? I am curious, within
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
Possible Duplicate: C# member variable initialization; best practice? Which is the right way to
Possible Duplicate: Do I cast the result of malloc? What is the Best Practice
Possible Duplicate: Who needs singletons? I always write with respect to best practice ,
Possible Duplicate: Array versus List<T>: When to use which? Is there any best practice

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.