Calling a constructor from another constructor C# with Example



Calling a constructor from another constructor C# with Example

public class Animal 
{ 
public string Name { get; set; } 
public Animal() : this("Dog") 
{ 
} 
public Animal(string name) 
{ 
Name = name; 
} 
} 
// dog.Name will be set to "Dog" by default. 
var dog = new Animal(); 
var cat = new Animal("Cat"); // cat.Name is "Cat", the empty constructor is not called. 

0 Comment's

Comment Form

Submit Comment