C# Programming - Constructors - Discussion

Discussion Forum : Constructors - General Questions (Q.No. 1)
1.
Which of the following statements is correct?
A constructor can be used to set default values and limit instantiation.
C# provides a copy constructor.
Destructors are used with classes as well as structures.
A class can have more than one destructor.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
18 comments Page 2 of 2.

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.

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.

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.

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

Srikanth said:   1 decade ago
C# provides copy constructor also.
So ans should be A &B

Hema said:   1 decade ago
C# provides copy constructor also.
So ans should be A &B

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

Tulsi said:   1 decade ago
C# also provide copy construvtor so ans. Should be a & b.


Post your comments here:

Your comments will be displayed after verification.