none
Observing Constructor Flow in C# RRS feed

  • Question

  • 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();
    	}
    }
    
    

    Monday, December 30, 2019 9:59 PM

Answers

  • Hi Arash,

    Thank you for posting here.

    In the first case, "new Car (10);" calls the second constructor of the Car class. This constructor references the fourth constructor. In this case, C # will call the referenced constructor first (The fourth), and then call  the second constructor.

    In the second case, although you assign a value to "_intensity", this syntax is not the syntax for calling the constructor, so the program calls the default parameterless construction.

    As for "- 10", please note the space between them, it is not a number but a string. You will see this more clearly if you assign a value to "_name".

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 31, 2019 3:18 AM

All replies

  • Hi Arash,

    Thank you for posting here.

    In the first case, "new Car (10);" calls the second constructor of the Car class. This constructor references the fourth constructor. In this case, C # will call the referenced constructor first (The fourth), and then call  the second constructor.

    In the second case, although you assign a value to "_intensity", this syntax is not the syntax for calling the constructor, so the program calls the default parameterless construction.

    As for "- 10", please note the space between them, it is not a number but a string. You will see this more clearly if you assign a value to "_name".

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 31, 2019 3:18 AM
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 7, 2020 8:57 AM
    Moderator