C# Class
When you define a class, you define a blueprint for a data type. This does not actually define any data, but it defines what the name of the class means, that is, what an object of the class consists of and what operations can be performed on that object. An object is an instance of a class. The methods and variables that make up a class are called members of the class.
Definition of the class
The definition of a class begins with the keyword class followed by the name of the class. The body of the class is enclosed in a pair of curly braces. The general form of a class definition is as follows:
<
access specifier
class
class_name
{
// member variables
<
access specifier
<
data type
variable1
;
<
access specifier
<
data type
variable2
;
...
<
access specifier
<
data type
variableN
;
// member methods
<
access specifier
<
return
type
method1
(
parameter_list
)
{// method body
}
<
access specifier
<
return
type
method2
(
parameter_list
)
{// method body
}
...
<
access specifier
<
return
type
methodN
(
parameter_list
)
{// method body
}
}
Please note:
- Access specifier
specifies the access rules for the class and its members. If not specified, the default access specifier is used. The default access specifier for a class is internal, and the default access specifier for a member is private。 - Data Type specifies the type of the variable; the return type
specifies the data type returned by the method. - To access a member of a class, you use the dot (.) operator.
- The dot operator links the name of the object and the name of the member.
The following example illustrates the concepts discussed so far:
Example
using
System
;
namespace
BoxApplication
{
class
Box
{
public
double
length
;
// length
public
double
breadth
;
// width
public
double
height
;
// height
}
class
Boxtester
{
static
void
Main
(
string
[
]
args
)
{
Box Box1
=
new
Box
(
)
;
// declare Box1 of type Box
Box Box2
=
new
Box
(
)
;
// declare Box2 of type Box
double
volume
=
0.0
;
// Volume
// details of Box1
Box1
.
height
=
5.0
;
Box1
.
length
=
6.0
;
Box1
.
breadth
=
7.0
;
// details of Box2
Box2
.
height
=
10.0
;
Box2
.
length
=
12.0
;
Box2
.
breadth
=
13.0
;
// volume of Box1
volume
=
Box1
.
height
Box1
.
length
Box1
.
breadth
;
Console
.
WriteLine
(
"Volume of Box1: {0}"
, volume
)
;
// volume of Box2
volume
=
Box2
.
height
Box2
.
length
Box2
.
breadth
;
Console
.
WriteLine
(
"Volume of Box2: {0}"
, volume
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Box1 的体积: 210
Box2 的体积: 1560
Member Functions and Encapsulation
A member function of a class is a function whose definition or prototype appears in the class definition, just like other variables. As a member of a class, it can operate on any object of the class and can access all members of that object's class.
Member variables are attributes of an object (from a design perspective), and they are kept private to achieve encapsulation. These variables can only be accessed using public member functions.
Let's use the concepts above to set and get the values of different class members in a class:
Example
using
System
;
namespace
BoxApplication
{
class
Box
{
private
double
length
;
// length
private
double
breadth
;
// width
private
double
height
;
// height
public
void
setLength
(
double
len
)
{
length
=
len
;
}
public
void
setBreadth
(
double
bre
)
{
breadth
=
bre
;
}
public
void
setHeight
(
double
hei
)
{
height
=
hei
;
}
public
double
getVolume
(
)
{
return
length
breadth
height
;
}
}
class
Boxtester
{
static
void
Main
(
string
[
]
args
)
{
Box Box1
=
new
Box
(
)
;
// declare Box1 of type Box
Box Box2
=
new
Box
(
)
;
// declare Box2 of type Box
double
volume
;
// Volume
// details of Box1
Box1
.
setLength
(
6.0
)
;
Box1
.
setBreadth
(
7.0
)
;
Box1
.
setHeight
(
5.0
)
;
// details of Box2
Box2
.
setLength
(
12.0
)
;
Box2
.
setBreadth
(
13.0
)
;
Box2
.
setHeight
(
10.0
)
;
// volume of Box1
volume
=
Box1
.
getVolume
(
)
;
Console
.
WriteLine
(
"Volume of Box1: {0}"
,volume
)
;
// volume of Box2
volume
=
Box2
.
getVolume
(
)
;
Console
.
WriteLine
(
"Volume of Box2: {0}"
, volume
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Box1 的体积: 210
Box2 的体积: 1560
Constructors in C#
The class's constructor is a special member function of a class that is executed when a new object of the class is created.
A constructor has exactly the same name as the class, and it has no return type.
The following example illustrates the concept of a constructor:
Example
using
System
;
namespace
LineApplication
{
class
Line
{
private
double
length
;
// Length of the line
public
Line
(
)
{
Console
.
WriteLine
(
"Object created"
)
;
}
public
void
setLength
(
double
len
)
{
length
=
len
;
}
public
double
getLength
(
)
{
return
length
;
}
static
void
Main
(
string
[
]
args
)
{
Line line
=
new
Line
(
)
;
// Set the line length
line
.
setLength
(
6.0
)
;
Console
.
WriteLine
(
"Length of the line: {0}"
, line
.
getLength
(
)
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
对象已创建
线条的长度: 6
Default Constructordoes not have any parameters. But if you need a constructor with parameters, you can have one; such a constructor is called aparameterized constructor. This technique helps you assign initial values to an object at the same time you create it, as shown in the following example:
Example
using
System
;
namespace
LineApplication
{
class
Line
{
private
double
length
;
// Length of the line
public
Line
(
double
len
)
// Parameterized constructor
{
Console
.
WriteLine
(
"Object created, length = {0}"
, len
)
;
length
=
len
;
}
public
void
setLength
(
double
len
)
{
length
=
len
;
}
public
double
getLength
(
)
{
return
length
;
}
static
void
Main
(
string
[
]
args
)
{
Line line
=
new
Line
(
10.0
)
;
Console
.
WriteLine
(
"Length of the line: {0}"
, line
.
getLength
(
)
)
;
// Set the line length
line
.
setLength
(
6.0
)
;
Console
.
WriteLine
(
"Length of the line: {0}"
, line
.
getLength
(
)
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
对象已创建,length = 10
线条的长度: 10
线条的长度: 6
Destructors in C#
The class's The destructor is a special member function of a class that is executed when an object of the class goes out of scope.
A destructor has the name of the class prefixed with a tilde (~); it returns no value and takes no parameters.
Destructors are used to release resources before ending the program (such as closing files, freeing memory, etc.). Destructors cannot be inherited or overloaded.
The following example illustrates the concept of a destructor:
Example
using
System
;
namespace
LineApplication
{
class
Line
{
private
double
length
;
// Length of the line
public
Line
(
)
// Constructor
{
Console
.
WriteLine
(
"Object created"
)
;
}
~Line
(
)
// Destructor
{
Console
.
WriteLine
(
"Object deleted"
)
;
}
public
void
setLength
(
double
len
)
{
length
=
len
;
}
public
double
getLength
(
)
{
return
length
;
}
static
void
Main
(
string
[
]
args
)
{
Line line
=
new
Line
(
)
;
// Set the line length
line
.
setLength
(
6.0
)
;
Console
.
WriteLine
(
"Length of the line: {0}"
, line
.
getLength
(
)
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
对象已创建
线条的长度: 6
对象已删除
Static Members of a C# Class
We can use the static The keyword defines a class member as static. When we declare a class member as static, it means that no matter how many objects of the class are created, there will be only one copy of that static member.
The keyword static It means there is only one instance of that member in the class. Static variables are used to define constants because their values can be retrieved by calling the class directly without creating an instance of the class. Static variables can be initialized outside a member function or the class definition. You can also initialize static variables inside the class definition.
The following example demonstratesStatic variable:
Example
using
System
;
namespace
StaticVarApplication
{
class
StaticVar
{
public
static
int
num
;
public
void
count
(
)
{
num
++;
}
public
int
getNum
(
)
{
return
num
;
}
}
class
StaticTester
{
static
void
Main
(
string
[
]
args
)
{
StaticVar s1
=
new
StaticVar
(
)
;
StaticVar s2
=
new
StaticVar
(
)
;
s1
.
count
(
)
;
s1
.
count
(
)
;
s1
.
count
(
)
;
s2
.
count
(
)
;
s2
.
count
(
)
;
s2
.
count
(
)
;
Console
.
WriteLine
(
"num of s1: {0}"
, s1
.
getNum
(
)
)
;
Console
.
WriteLine
(
"num of s2: {0}"
, s2
.
getNum
(
)
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
s1 的变量 num: 6
s2 的变量 num: 6
You can also declare amember functionas static. Such a function can only access static variables. A static function exists before an object is created. The following example demonstratesstatic function:
Example
using
System
;
namespace
StaticVarApplication
{
class
StaticVar
{
public
static
int
num
;
public
void
count
(
)
{
num
++;
}
public
static
int
getNum
(
)
{
return
num
;
}
}
class
StaticTester
{
static
void
Main
(
string
[
]
args
)
{
StaticVar s
=
new
StaticVar
(
)
;
s
.
count
(
)
;
s
.
count
(
)
;
s
.
count
(
)
;
Console
.
WriteLine
(
"Variable num: {0}"
, StaticVar
.
getNum
(
)
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
变量 num: 3