C# Programming - Constructors - Discussion
Discussion Forum : Constructors - General Questions (Q.No. 1)
1.
Which of the following statements is correct?
Discussion:
18 comments Page 1 of 2.
Sunil said:
1 decade ago
class Person
{
private string name;
private int age;
// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}
// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
}
class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);
// Create another new object, copying person1.
Person person2 = new Person(person1);
System.Console.WriteLine(person2.Details);
}
}
{
private string name;
private int age;
// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}
// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
}
class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);
// Create another new object, copying person1.
Person person2 = new Person(person1);
System.Console.WriteLine(person2.Details);
}
}
Amaan said:
1 decade ago
C sharp does provide a copy construct but the prime work for the constructor is to assign values to the variables as c# you cannot use a variable without declaring assigning a value where the automatic initialization happens in the case of instance variable or global variable.
Sundar said:
1 decade ago
@Tulsi
Unlike some languages, C# does not provide a copy constructor. If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.
Source: http://msdn.microsoft.com/en-us/library/ms173116(v=vs.80).aspx
Unlike some languages, C# does not provide a copy constructor. If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.
Source: http://msdn.microsoft.com/en-us/library/ms173116(v=vs.80).aspx
Akash said:
1 decade ago
Generally, constructors are of three types. But C# doesn't support copy constructor.
- Default Constructor(Non-Parametrized constructor).
- Parametrized Constructor.
- Copy Constructor.
- Default Constructor(Non-Parametrized constructor).
- Parametrized Constructor.
- Copy Constructor.
Gaurav Balyan said:
1 decade ago
C# does not provide Copy Constructor and if you need one you have to do it by using a method, and you can also have a ICloneable Interface to implement the functionality.
RAJEEV RANJAN KUMAR(RAJU) said:
1 decade ago
C# provides three types of constructor:
1st 1 is: Default Const.
2nd 1 is: Param Const.
And 3rd 1 is: Copy Const.
So Answer Should be A & B.
1st 1 is: Default Const.
2nd 1 is: Param Const.
And 3rd 1 is: Copy Const.
So Answer Should be A & B.
Arul said:
1 decade ago
C# provide a copy constructor and C# class have more than one constructor. So answer B & D.
Neeraj Sharma said:
1 decade ago
C# doesn't provide a copy constructor for objects, but you can write one yourself.
Bhavani said:
1 decade ago
C# doesn't provide copy constructor, every class have implicit constructor.
David said:
10 years ago
Constructors is automatically invoked when the class objects is initiated.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers