C Sharp/Constructors

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Home

Basics
Home
introduction
Overview
Statements
Data Types
Variables
Operators
Flow Control
Variables II
Functions
C# - Classes and Objects I

Dates and Times
Conversions

Advanced
Classes II
Inheritance
GUI
Namespaces
Exceptions
Threads
ADO.NET
Generics

[edit] How to use C# constructors

When you use the new operator it may be necessary to initialize field values to custom values. It is not necessary to define a C# constructor a default constructor is called if there are no programmer defined constructors. The default constructor does not do anything except creates your object.

  • A constructor is a method that has the same name as the class name

Lets take a look at how to define a constructor of our own.


using System;
class Program
{
    public class test
    {
        public int testvariable;
        public test(int test)   //Constructor
        {
            this.testvariable = test;
        }
    }
    static void Main()
    {
        test ClassObject = new test(81);    //call constructor
 
        //print class field member
        Console.WriteLine(ClassObject.testvariable);
        Console.Read();
    }
}

Output:

81


  • public test(int test) has the same name as the class and it does not specify a return type because constructors do not return values.
  • new test(81); calls the constructor and the constructor simply assigns the field member to its local variable.

[edit] How to Overload C# Constructors

Just like C# functions and methods, constructors can be overloaded too. There my be different situations where you may have two different situations and need two different constructors.

  • You must match parameters when using constructors
  • You can have as many constructors as you need

Lets take a look at an example:


using System;
class Program
{
    public class test
    {
        public string testvariable;
        public test()   //Default Constructor
        {
            this.testvariable = "John Doe";
        }
 
        public test(string test)   //Overloaded Constructor
        {
            this.testvariable = test;
        }
    }
    static void Main()
    {
        test ClassObject = new test();    //call default constructor
        test ClassObject2 = new test("Jeannette");  //call constructor
 
        //print class field member
        Console.WriteLine(ClassObject.testvariable);
        Console.WriteLine(ClassObject2.testvariable);
        Console.Read();
    }
}


Output:

John Doe
Jeannette

[edit] C# Copy Constructor

When writing your C# application you may need to copy class objects to other class objects. To do this you need a copy constructor because there is no build in way to do this. The copy constructor simply copies all the field values of the right side to the left side.


Call a copy constructor like this: test ClassObject2 = new test(ClassObject);

  • the new keyword must be used to call the copy constructor
  • The parameter to pass to the constructor must be the value of the leftside object

Lets take a look at example code:

using System;
class Program
{
    public class test
    {
        public string testvariable = "";
        public test(string test)   //Constructor
        {
            this.testvariable = test;
        }
 
        public test(test leftside)  //Copy constructor
        {
            this.testvariable = leftside.testvariable;
        }
    }
    static void Main()
    {
        test ClassObject = new test("Elaine");  //create first object
        test ClassObject2 = new test(ClassObject);  //Call copy constructor
 
 
        //print class field member
        Console.WriteLine(ClassObject.testvariable);
        Console.WriteLine(ClassObject2.testvariable);
        Console.Read();
    }
}

Output:

Elaine
Elaine

Note: you can only copy objects that are created from the same class