OOPS Corner : Instance Constructors

原创 2004年09月19日 09:52:00

In this section we will see about instance constructors.

Instance Constructors

Instance constructors are responsible for setting an object instance to its initial state. Instance constructors of classes are invoked whenever an instance of a class is created.

Syntax of a simple instance constructor is as follows

ConstructorName()
{
...constructor body
}

Note that the name of the constructor should be same as that of the class and constructors cannot return any value not even void. Failing these conditions will result in a compiler error.

Example of an Instance Constructor is given below

class MyClass
{
public MyClass()
{
Console.WriteLine(“I am Constructor”);
}
}

Constructors can be overloaded meaning that a class can have several constructors with each constructor taking a different set of parameters as shown in sample below

class MyClass
{
public MyClass()
{
Console.WriteLine("No Parameter");
}

public MyClass(string param1)
{
Console.WriteLine("One Parameter");
}

public MyClass(string param1, int param2)
{
Console.WriteLine("Two Parameters");
}
}

If a class does not contain any instance constructor, a default parameter less instance constructor will be provided automatically. This default constructor will initialize the object fields to their default values.

Variable initializers are executed automatically before constructor body for example

class MyClass
{
int x=5;
int y;
public MyClass()
{
Console.WriteLine("X={0},Y={1}",x,y);
}
}

class startUP
{

publicstaticvoid Main()
{
MyClass MC=new MyClass();
}
}

will result in X=5,Y=0 as x is initialized to 5 and y is initialized to the default value of 0.

8.7.8 Instance constructors

An instance constructor is a member that implements the actions required to initialize an instance o...
  • masterall
  • masterall
  • 2005年11月30日 19:12
  • 581

17.10 Instance constructors

An instance constructor is a member that implements the actions required to initialize an instance o...
  • masterall
  • masterall
  • 2006年05月21日 02:50
  • 684

Corner case

 Corner CaseA corner case (or pathological case) is a problem or situation that occurs only outside ...
  • Eleve
  • Eleve
  • 2008年01月23日 15:10
  • 1288

Harris corner detector algorithm

Harris and Stephens[3] improved upon Moravec's corner detector by considering the differential of t...
  • fanbird2008
  • fanbird2008
  • 2013年12月21日 19:36
  • 1814

error C2533: ' ::{ctor}' : constructors not allowed a return type

error C2533: ' ::{ctor}' : constructors not allowed a return type 出现这个错误是因为在定义的类体中,在最后少了个分号“;...
  • andkobe
  • andkobe
  • 2011年02月13日 15:07
  • 4726

Avoid non-default constructors in fragments

在使用Android Studio时Fragment类报 Avoid non-default constructors in fragments
  • yyqasg
  • yyqasg
  • 2014年11月13日 12:38
  • 2271

oops异常定位方法

关于如下异常信息: [   21.813421] Unable to handle kernel NULL pointer dereference at virtual address 0000000...
  • huangxin8707
  • huangxin8707
  • 2015年07月08日 16:59
  • 598

oops信息的分析

oops是英语口语"糟糕"的意思,当LINUX 内核发生严重错误时,比如内存段错误时,将会提示一大段信息。 就提示 Oops(也称 panic),因此得名,Oops提示信息相当多,包括出问题时的...
  • zhangchiytu
  • zhangchiytu
  • 2012年12月17日 01:18
  • 3716

Linux -----oops错误

Chinaunix首页 | 论坛 | 认证专区 | 博客 登录 | 注册       博文    博主    ly44770 arm+linux学习中ly44770.blog...
  • heixiaolong7
  • heixiaolong7
  • 2015年12月03日 21:10
  • 389

Autofuc None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructor

最近项目中使用Autofuc实现依赖注入时出现错误: None of the constructors found with 'Autofac.Core.Activators.Reflectio...
  • shenwenbin0106
  • shenwenbin0106
  • 2017年03月17日 16:02
  • 1358
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OOPS Corner : Instance Constructors
举报原因:
原因补充:

(最多只允许输入30个字)