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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:24:51+00:00 2026-05-16T17:24:51+00:00

Suppose I initialize members of a class like this: class A { public int

  • 0

Suppose I initialize members of a class like this:

class A 
{
    public int i=4;
    public double j=6.0;
}

Does the compiler generate a default constructor in this situation?

In general, I know that a constructor may initialize the value of class instance variables and may also perform some other initialization operations appropriate for the class. But in the above example, I have initialized the value of i and j outside of a constructor. In this situation, does the compiler still generate a default constructor? If so, what does the default constructor do?

  • 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-16T17:24:52+00:00Added an answer on May 16, 2026 at 5:24 pm

    The compiler still generates a default constructor in this case. The constructor handles the initialization of i and j. If you look at the IL this is evident.

    .class auto ansi nested private beforefieldinit A
       extends [mscorlib]System.Object
    {
       .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
       {
          .maxstack 8
          L_0000: ldarg.0 // pushes "this" onto the stack
          L_0001: ldc.i4.4 // pushes 4 (as Int32) onto the stack
          L_0002: stfld int32 TestApp.Program/A::i // assigns to i (this.i=4)
          L_0007: ldarg.0 // pushes "this" onto the stack
          L_0008: ldc.r8 6 // pushes 6 (as Double) onto the stack
          L_0011: stfld float64 TestApp.Program/A::j // assigns to j (this.j=6.0)
          L_0016: ldarg.0 // pushes "this" onto the stack
          L_0017: call instance void [mscorlib]System.Object::.ctor() // calls the base-ctor
          /* if you had a custom constructor, the body would go here */
          L_001c: ret // and back we go
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class with 3 constructors, a default (no argument) constructor, a
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
Suppose I have a class 'Application'. In order to be initialised it takes certain
Suppose I have a class module clsMyClass with an object as a member variable.
I would like to write an object generator for a templated RAII class --
suppose you have two (or more) classes with private member vectors: class A {
Suppose I have a class with private memebers ptr , name , pname ,
I have created an abstract base class Animal which has public virtual abstract method

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.