08-DotNet UserDefinedDataTypeMembers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Constructors

Constructors Overview
Whenever a class or struct is instantiated using “new” keyword, its constructor is called implicitly.
Constructors enable the programmer to set default values and limit instantiation.
Default / Parameterless Constructors :
If you do not provide a constructor for your class, C# will create one by default that instantiates the
object and sets member variables to the default values shown in next slide.

C# VB.Net

Usage
See Also

Default Values Table

C# Value Type VB.Net Value Type Default value


bool Boolean false
byte Byte 0
char Char '\0'
decimal Decimal 0.0M
double Double 0.0D
The value produced by the
enum Enum expression (E)0, where E is the
enum identifier.
float Single 0.0F
int Integer 0
long Long 0L
sbyte SByte 0
short Short 0
The value produced by setting all
value-type fields to their default
struct Struct
values and all reference-type fields
to null.
uint UInteger 0
ulong UIong 0
ushort UShort 0
Constructors
Parameterized Constructors
 A class or struct may have multiple constructors that take different set of arguments.

C# VB.Net

Usage
Constructors
Instance Constructors
 Instance constructors are used to create and initialize any instance member variables when
you use the new expression to create an object of a class.

C# VB.Net

Usage
Constructors
Static Constructors
 A static constructor is used to initialize any static data, or to perform a particular action that
needs to be performed once only. It is called implicitly for the first instance; it’ll not be called for the
subsequent instances. Note: Static members cannot access instance members.

C# VB.Net
Constructors
Private Constructors
 A private constructor is a special instance constructor. It is generally used in classes that
contain static members only. It is used in Singleton Pattern, Helper class implementation.

C# VB.Net

Usage
Constructors
Copy Constructors
 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.

C# VB.Net

Usage
Destructors
Destructors
Destructors are used to destruct instances of classes. This is called by the GC during reclaiming the
memory.
Destructors cannot be defined in structs. They are only used with classes.
A class can only have one destructor.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.
C# VB.Net
Properties
Overview
A property is a member that provides a flexible mechanism to read, write, or compute the value of a
private field. Properties can be used as if they are public data members, but they are actually special
methods called accessors. This enables data to be accessed easily and still helps promote the safety
and flexibility of methods.

C# VB.Net

Usage

You might also like