Hello,
For what purpose we are using constructor and destructor in a Program.....[Constructor Overloading]
We wanna use this mandatory...! Please clarify me....
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Aug 6, 2013, 12:51 AM EDT
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
public virtual void draw()
{
Console.WriteLine("Base class draw method called");
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
public class Employee : Person
{
public override void draw()
{
Console.WriteLine("Child class draw method called");
}
}
Person p = new Person();
p.draw();
Employee emp = new Employee();
emp.draw();
The person object will call the draw method from person and Employee object method will call the draw method from employee class.
To override the method in child class, in the base class that method should be marked as virtual then you can override it in the child class.
If the method is not marked as virtual in the base class, still can override it by using "new" keyword instead of override.
public new void draw()
{
Console.WriteLine("Child class draw method called");
}
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 5, 2013, 9:44 PM EDT
Please refer the below link
In Constructor
http://www.dotnetspider.com/forum/171656-constructor-uses-Purpose.aspx
In Destructor
http://www.c-sharpcorner.com/UploadFile/chandrahundigam/UnderstandingDestructors11192005021208AM/UnderstandingDestructors.aspx
Regards,
Iftikar
Posted Aug 5, 2013, 10:32 PM EDT
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
//You can creat object for person in multiple ways,
Person person = new Person(); //Which calls default constructor
person = new Person("Murali"); //Which calls single argumented constructor
person = new Person("Murali",10); //Which calls two argumented constructor
//Destructors are used to dispose an object.To call the destructor, call GC.Collect();
System.GC.Collect();
Vijay134 reputationPosted Aug 5, 2013, 11:40 PM EDT
How can use method overriding and loading using constructor... plz give example
Posted Aug 5, 2013, 11:44 PM EDT
Person person = new Person(); //Which calls default constructor
person = new Person("Murali"); //Which calls single parameterized constructor
person = new Person("Murali",10); //Which calls two parameterized constructor
Posted Aug 5, 2013, 11:55 PM EDT
You can override base class methods in the child class, but not the constructors.
Vijay134 reputationPosted Aug 6, 2013, 12:43 AM EDT
Give example for method loading and overriding using class
Vijay134 reputationPosted Aug 6, 2013, 2:50 AM EDT
Is it access modifier ? give samples sir......
public new void draw() { }
Posted Aug 6, 2013, 3:12 AM EDT
public class Person
{
public string Name {get;set;}
public int Id{get;set;}
//Default constructor or silent constructor
public Person()
{
Console.WriteLine("Default constructor called");
}
public Person(string Name)
{
this.Name = Name;
Console.WriteLine("One arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name);
}
public Person(string Name,int Id)
{
this.Name = Name;
this.Id = Id;
Console.WriteLine("Two arguemented constructor called");
Console.WriteLine("Person name is "+ this.Name + " id " + this.Id);
}
public void draw()
{
Console.WriteLine("Base class draw method called");
}
~Person()
{
Console.WriteLine("Destructor called");
}
}
public class Employee : Person
{
public new void draw()
{
Console.WriteLine("Child class draw method called");
}
}
Person p = new Person();
p.draw();
Employee emp = new Employee();
emp.draw();
The derived class method is preceded with the new keyword, the method is defined as being independent of the method in the base class. The override modifier extends the base class method, and the new modifier hides it.
To know more about on the new and override keyword,
http://msdn.microsoft.com/en-us/library/ms173153.aspx
Vijay134 reputationPosted Aug 6, 2013, 4:48 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2013, 4:59 AM EDT
However, if you don't use it when it should be used, then the compiler will issue a warning.
Similarly, if you use it when it shouldn't be used, then the compiler will warn you about that as well.
However, because you can't both hide and override an inherited member at the same time, the combination 'new override' is not allowed and gives a compiler error.
Vijay134 reputationPosted Aug 6, 2013, 5:08 AM EDT
Vulpes plz mention some examples what u said
""
'new' to hide an inherited member is never necessary.
However, if you don't use it when it should be used, then the compiler will issue a warning.
Similarly, if you use it when it shouldn't be used, then the compiler will warn you about that as well.
""
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2013, 5:19 AM EDT
public new void draw()
try removing 'new' and then study the compiler output when you rebuild.
Similarly, add this method to the Employee class and look at the compiler output:
public new void draw2()
{
}