0

Where to write the second Console.WriteLine() ?

I am confused , Should the Console.WriteLine(name); have been put in the static void instead of public dogs (name) where we should just put return name ? Any help please guys , Thank in advance class Dog { public Dog() { Console.WriteLine(1); } public Dog(string name) { Console.WriteLine(name); } } static void Main(string[] args) { Dog d = new Dog("2"); } // output is 2

5th Jun 2022, 6:14 PM
Mustafa Azzoz
Mustafa Azzoz - avatar
2 Answers
+ 4
What do you except as an output? This code is right. You have 2 constructor which will be explicitly call when you create object.
5th Jun 2022, 6:25 PM
AÍąJ
AÍąJ - avatar
+ 3
For simple classes like this I would avoid using the Console.WriteLine() into the dog class. The following code should be much better: class Dog { public string Name; public Dog() { } public Dog(string name) { Name = name; } } static void Main(string[] args) { Dog d = new Dog("2"); Console.WriteLine(d.Name); }
5th Jun 2022, 6:32 PM
Apollo-Roboto
Apollo-Roboto - avatar