This Post From My Old Blog
Hello, Here I’ll tell you some notes about constructors and some tricks that you may face while using constructors.
* Constructors never define return value (that what distinguish constructor from method).
* If you didn’t define a constructor in your class, it will be automatically supplied with the default constructor. But if you defined a constructor with parameters, the supplied default constructor will be removed and you won’t be able to use it until you explicitly define it in your class.
* If you defined your class as internal and a constructor inside it as public, so you can’t new use it in another assembly. Although the constructor is public, but the class is internal (accessed only inside the assembly that define it).
That’s a class definition in an assembly named Constructors
using System;
namespace Constructors
{
class parent
{
public parent()
{ Console.WriteLine(“Hello”); }
}
}
Try to use parent class in another assembly. Create console application and make reference to Constructors.dll
using System;
using Constructors;
namespace usingConstructor
{
class Program
{
static void Main(string[] args)
{
parent p = new parent();
}
}
}
You’ll receive error.
* The default access modifier for the constructor is private (like methods).
* If a constructor defined as private, you can’t instanciate it outside the class. And also you can’t define a child class inherit from parent class.Why???!!
Have a look at this code
namespace Constructors
{
public class parent
{
parent()
{ Console.WriteLine(“Hello”); }
}
public class child:parent
{
}
}
When you define child class inherit from parent class, the child class not only implicitly define default constructor but also this default constructor is implicitly call the default constructor of the parent class.
And here in child class, there isn’t constructor defined explicitly, then it will be implicitly supplied with the default constructor. This default constructor will call the default constructor of the parent class. The error will be raised at this step, because the default constructor in parent class is private, so child class can’t see it.
* Constructors can take any access modifier. Protected access modifier with constructor allow child class to inherit from parent class without any errors. But if you defined constructor as protected, you can’t access it through object variable. Such as protected access modifier with methods. As I described it before.
So if you try to write this line of code inside child class, you’ll receive an error
parent p = new parent();
* Now after defining parent class as public and its constructor as protected, try to create an object from child class inside Main in the console application that we created miniuts ago.
child c = new child();
the output is : Hello
* The first statement is executed inside the constructor is calling the default constructor of the base class(be aware that if your class didn’t explicitly inherit from a base class, your class will implicitly inherit from object class. And then every constructor will call object’s default constructor).
Try this
namespace Constructors
{
public class parent
{
protected parent()
{ Console.Write(“Hello,”); }
}
public class child:parent
{
public child(){}
public child(string yourName)
{
Console.WriteLine(yourName);
}
}
}
And inside the Main, try this
static void Main(string[] args)
{
child c = new child(“Amira”);
Console.WriteLine();
child c1 = new child();
Console.Read();
}
-First line in Main, I created an object from child with a string argument “Amira”
Here it will invoke the constructor of child class that take a string and write it. But before executing this statement inside child’s constructor, it’ll invode the parent’s default constructor(which write Hello,) so the result of that statement will be Hello, Amira
-Second line in Main, insert new line
-Third line in Main: I created another object from child class, but here it will invoke the custom default constructor with no argument. This constructor as I defined do nothing except the implicit call for parent’s default constructor. So the output will be Hello,
* If parent class has no default constructor, then when the constructors inside child class try to call the parent’s default constructor, they wont find it, you will receive error. Try to change the definion of the constructor inside parent class
public class parent
{
protected parent(string s)
{ Console.Write(“Hello,”); }
}
Here the default constructor is removed, and you’ll receive an error in that statement inside child class
public child(){} //error
* As I said before, the first statement is executed at child’s constructor is thr implicit call to paren’s default constructor. You can explicitly invoke the parent’s constructor in child’s constructors through base keyword (and the explicit call allow you to invoke any constructor defined at parent class), and that explicit call will be also the first statement executed when you instanciate the child class. If you supported child constructor with explicit call, the implicit one will be removed.
Try this
namespace Constructors
{
public class parent
{
protected parent(string str)
{ Console.Write(“Hello,” + str); }
}
public class child:parent
{
public child()
: base(“Amira”)
{ }
public child(string yourName)
: base(yourName)
{ }
}
}
Try again what we wrote in main the output will be
Hello, Amira
Hello, Amira
* Also you can make explicit call to a constructor that is defined inside the class through this keyword(that is instead of the explicitly call to parent’s constructor or instead of the implicitly call to parent’s default constructor).
So change child class definition to
public class child:parent
{
public child()
: base(“Amira”)
{ }
public child(string yourName):this()
{ Console.WriteLine(“\nBye”); }
}
Run the console application again, the output will be
Hello, Amira
Bye
Hello, Amira
* you can overload the constructor, the code you wrote before, you overloded child’s custom default constructor with an overloaded constructor with one parameter.
* Constructors can be defined as static, but you allowed to define only a single static constructor. And this static constructor doesn’t take any access modifier and also doesn’t take any parameters.
* Static constructor executes only one time, and executes before the other constructors.
Here inside StaticConstructor class, I defined static constructor which prints a message to inform the user when the static constructor is invoked and also to initialize static field (y)
public class StaticConstrucor
{
public int x;
public static int y;
static StaticConstrucor()
{
Console.WriteLine(“static constructor is invoked now”);
y = 2;
}
public StaticConstrucor()
{
Console.WriteLine(“x={0} and y={1}”,x,y);
}
public StaticConstrucor(int x)
{
this.x = x;
Console.WriteLine(“x={0} and y={1}”, x, y);
}
}
* The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller. Try this snippet of code inside the Main method
StaticConstrucor s = new StaticConstrucor();
StaticConstrucor s1 = new StaticConstrucor(3);
StaticConstrucor.y = 9;
StaticConstrucor s2 = new StaticConstrucor();
Here the static constructor invoked only once when I instanciated StaticConstructor class. So the output will be
static constructor is invoked now
x=0 and y=2
x=3 and y=2
x=0 and y=9
Also it can be invoked automatically before invoking the static member y, try only this statement
Console.WriteLine(StaticConstrucor.y);
Here the output will be
static constructor is invoked now
2
That all what I remembered about constructors. I wish I covered this topic well, and I hope that helps
Amira