-2

After reading Meanings of declaring, instantiating, initializing and assigning an object it talks about what initializing a variable means. But it doesn't explain what "initializing" an instance of a class means.

public class Test
{
   public static void Main()
   {
      Person person1 = new Person();
   }
}

public class Person
{
   // body
}

What does it mean to initialize an instance of a class?

  • 1
    I think some terminology gets mixed up and interchanged and used incorrectly. I would say initializing is where you actually assign internal initial variable values when the instance of the class is created. – Nick.Mc Dec 31 '21 at 04:29
  • In your example, it would be like making a new Person, with its initial conditions, you are also allocating the space for that person (I think). – Gwendal Delisle Arnold Dec 31 '21 at 04:30
  • I think it is equivalent to "initialize the data members of the new object". – shingo Dec 31 '21 at 04:40
  • A class is a definition of object - it describe what data it holds and what operations it can perform. An instance is an actual allocation of memory based on the class definition. To initialize the instance is to set the memory allocated to the instance to the default values defined on the fields or within the constructor. – Enigmativity Dec 31 '21 at 05:34
  • @XUnitDown Yes. – shingo Dec 31 '21 at 05:34
  • @shingo - Plus run the constructor. – Enigmativity Dec 31 '21 at 05:35

1 Answers1

1

Yeah, I don't like the "initialize" of the linked answer so much either, because it really only talks about giving a value to a single variable and doesn't really draw any distinctions between instantiation and assignment (the same lines of code are found in all of them) so for me it's a bit vague. We do have more specific processes (especially these days of modern c# syntax) when we talk about initialization


Initialize usually means "to give a created instance some initial values". Your class Person has nothing to initialize, so you could say that just by making it anew(instantiating) you've also done all the initialization possible and it's ready for use

Let's have something we can set values on

public class Person{
  public string Name {get;set;}
  public string Address {get;set;}

  public Person(string name){
    if(name == null) throw new ArgumentNullException(nameof(name));
    Name = name;
  }
}

Initializing as part of construction:

p = new Person("John");

Constructors force us to supply values and are used to ensure a developer gives a class the minimum set of data it needs to work.. a Person must have a name. Address is optional. We have created a person with the name initialized to John

Initializing post construction

You can give an instance additional (optional) values after you construct it, either like

p = new Person("Bill");
p.Address = "1 Microsoft Way";

Or

p = new Person("Bill"){
  Address = "1 Microsoft Way"
}

Which is a syntactic sugar the compiler unrolls to something like the first. We refer to everything in the { } brackets of the second example as "an object initializer". An important distinction here though is that the first form (p.Address=...) is not considered to be initialization by the compiler. If you made the address property like:

public string Address {get;init;}

Then it can only be set in a constructor or in an object initializer, which is the latter form above. The p.Address=... form would result in a compiler error if the property were declared with init

Props set just after construction are part of the initialization process (as an English/linguistic thing) though I wouldn't call it init if it was any further down the line, such as

p = new Person("Sam");

string addr = Console.ReadLine();

p.Address = addr; //not initialization

You might find cases where people talk about initialization in the sense for "the first time a variable or property is given a value" but that's also more a linguistic/English thing than a c# thing


The compiler knows how to perform other initialization, so we also call things like this "an initializer":

string[] x = new string[] {"a","b","c"};

The process of giving the array those 3 values is initialization, and the compiler will even take the type of the first argument and use it to make the array type, so an array can be type declared and ignited from the data:

var x = new[] {"a","b","c"};
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Sure, you could say that. Constructors are always run, object initializers are optional, so "via only object init'r" isn't really that logical but you could perhaps claim that an empty constructor does no init - I doubt the majority of regular people would care enough to take issue with it – Caius Jard Jan 02 '22 at 01:32