Observing Constructor Flow in C#

  • Thread starter Thread starter Arash_89
  • Start date Start date
A

Arash_89

Guest
Hello,

First and second syntax have different result. What's different between these?

Thanks


using System;

class Car
{
private int _intensity;
private String _name;
public Car() { Console.WriteLine("Default"); }
public Car(int intensity) : this(intensity, " ") { Console.WriteLine("in Intensity CTOR"); }
public Car(String name) : this(0, name) { Console.WriteLine("In name CTOR"); }
public Car(int intensity, String name)
{
Console.WriteLine("in Master CTOR");
_intensity = intensity;
_name = name;
}

public void Print() => Console.WriteLine($"{_name} - {_intensity}");


public static void Main()
{
Console.WriteLine("First");
Car car = new Car(10); // FIRST
//Console.WriteLine("Second");
//Car car = new Car() { _intensity = 10 }; // SECOND
car.Print();
}
}


1522767.jpg

1522766.jpg

Continue reading...
 
Back
Top