Professional Documents
Culture Documents
Introduction
Syntax of a class
Syntax of a class Access Modifiers
Members of a class
Constructors
Destructors
Fields
Constants
Methods
Property
Assembly
Project
Project
File
File
Classes
File
File
Classes
& TypesFile
Classes
Classes
& Types
File
Classes
&
Types
& Types
Classes
& Types
& Types
2
Introduction - Content of an
Application
C# applications contain one or more classes
C# applications can span across many files
All applications have a starting point.
Function Main is the starting point of a C#
Console application and must be in one of
the classes
You can put a Main in every class. If so, then
you must specify which main function is the
starting point of your application. This is
commonly done to test a class
Unlike Java, the file name does not have to
be name of the class that contains the main
function
3
Common Terms in
Object Oriented Programming
Polymorphism
Encapsulation
Inheritance
What is a Class?
Class is the standard unit of programming in C#
It is like a blueprint or template contains all
the information about the type
Objects are instantiated (created) from a class
An object is an (unique) instance of a class
For example, a house (object) is an instance of a
blueprint (class). Each house is a separate
entity, but can be made from the same blueprint
Another analogy. When you create a variable,
the type (for example int) can be considered
as the class and the variable would be the
object. You first create an object of a type (class)
and then work with the object (in most cases)
Syntax of a Class
[attributes] [modifiers] class identifier
[:baselist]
{class body}[;]
For now, we will skip the attributes, modifiers and the baselist
A Class (body) consists of fields (types), properties and
methods
Types can be primitive types or user defined types
Types can be value or reference types
A Class can contain nested classes or use other classes as
types
C# applications must contain one or more classes
A C# class is not restricted to a single file (unlike C++, like
Java). They span across multiple files. This requires attaching
a partial attribute to the class
A Simple Class
10
Members of a Class
Constructors
Destructors
Constants
Fields
Methods
Properties
Indexers
Operators
Events
Delegates
Classes
Interfaces
Structs
11
Members of a Class
Instance Variables and Properties
Variables declared in the body of a method are
known as local variables
When a method terminates, the values of its
local variables are lost
Variables declared in the body of a class, outside
the methods are known as instance variables
(also known as attributes or fields) are
represented as variables in a class declaration
Each object has its own copy of instance
variables, but methods are shared by all objects
12
13
14
15
UML Diagram
Unified Modeling Language (UML)
A standard, general-purpose modeling language used in
software engineering for designing software
It uses a set of graphical notations to create an abstract model
of a specific system
16
Namespace in Class
Classes in the same project are considered to be
in the same namespace
using indicates that the application uses classes
in another namespace
Without using, we would have to write the fully
qualified class name
System.Console.WriteLine ( "Please enter the course
name:" );
17
Properties
Contain special methods called get and set, to
access classs (private) data. These methods are
also known as accessor methods
Get allows you to read back the value of the fields
Set allows you to write data to the fields
Properties simulate (looks and act like) public fields.
This enables data to be accessed easily while still
providing the safety and flexibility of methods
Properties can be
Read-only Only contains a get method
Write-only Only contains a set method
Read-write Contains both get and set methods
18
19
20
21
Auto-implemented Properties
Auto-implemented properties were introduced in
C# Version 3.0. Following is the old syntax (C#
private
string courseName; // course name for this GradeBook
V2.0)
// property to get and set the course name
public string CourseName
{
get
{
return courseName;
} // end get
set
{
courseName = value;
} // end set
} // end property CourseName
Field
Property
Name
22
23
Fig. 4.9 | UML class diagram indicating that class GradeBook has a public CourseName
property of type string and one public method.
24
25
Processing
Processes the input or performs some action (execute code)
Output or Result
May return data as a result of processing the information
26
Constructors
Destructors
Operators (Discussed in advanced topics)
General Purpose Methods
27
28
29
30
31
32
33
34
Fig. 4.15 | GradeBook constructor used to specify the course name at the time each
GradeBook object is created. (Part 1 of 2).
35
Fig. 4.15 | GradeBook constructor used to specify the course name at the time each
GradeBook object is created. (Part 2 of 2).
36
Fig. 4.16 | UML class diagram indicating that class GradeBook has a constructor with a name
parameter of type string.
37
38
Account (Fig.4.17)
An instance variable represents
each Accounts own balance.
39
40
41
Local variable
depositAmount is
not initialized to 0
but will be set by
the users input.
Obtaining input
from the user.
Obtaining the
deposit value
from the user.
42
Outputting the
balances of
both Accounts.
43
44
44
45
Format Specifiers
45
Example 4.16
public class AccountTest
{
public static void Main( string[] args )
{
Account account1 = new Account( 50.00M ); // create Account object
Account account2 = new Account( -7.53M ); // create Account object
// display initial balance of each object using a property
Console.WriteLine( "account1 balance: {0:C}",
account1.Balance ); // display Balance property
Console.WriteLine( "account2 balance: {0:C}\n",
account2.Balance ); // display Balance property
decimal depositAmount; // deposit amount read from user
// prompt and obtain user input
Console.Write( "Enter deposit amount for account1: " );
depositAmount = Convert.ToDecimal( Console.ReadLine() );
Console.WriteLine( "adding {0:C} to account1 balance\n",
depositAmount );
account1.Credit( depositAmount ); // add to account1 balance
46
Example 4.16
// display balances
Console.WriteLine( "account1 balance: {0:C}",
account1.Balance );
Console.WriteLine( "account2 balance: {0:C}\n",
account2.Balance );
// prompt and obtain user input
Console.Write( "Enter deposit amount for account2: " );
depositAmount = Convert.ToDecimal( Console.ReadLine() );
Console.WriteLine( "adding {0:C} to account2 balance\n",
depositAmount );
account2.Credit( depositAmount ); // add to account2 balance
// display balances
Console.WriteLine( "account1 balance: {0:C}", account1.Balance );
Console.WriteLine( "account2 balance: {0:C}", account2.Balance );
} // end Main
} // end class AccountTest
47
Overloaded Methods
If a method has the same name and a different
signature, it is considered as an overloaded
method
Each method must have the same name but it
must also be unique. This true if
Number of parameters are different
Type of parameters are different
Return type does not matter
Time()
Time(int h )
Time(int h, int m )
Time(int h, int m, int s )
48
49
50
51
Struct
System.Enum
Primitive Types (13)
Boolean
int
Byte
Single
Decimal
Double
User Defined
System Defined
52
Namespaces
Namespaces
namespace NSpace1
{
class C1
{
class C2
{
}
}
namespace NSpace2
{
class C2
{
}
}
}
// NSpace1.C1
// NSpace1.C1.C2
// NSpace1.NSpace2
// NSpace1.NSpace2.C2
53
54
Nested Namespaces
Example of nested namespaces
Needed when more than one class names have
to be same or when you class name is in conflict
with a system class name
using NSpace1;
C1 a;
NSpace1.C1 b;
C2 c;
NSpace1.NSpace2.C2 d;
C1.C2 e;
// Error! C2 is undefined
// One of the C2 classes
// The other one
55
// Refers to N1.N2.C1
// Refers to N1.N2.C1
56
57
Access
Public
protected
Internal
To all code within the assembly. This applies to methods and data
too. If methods and fields are declared internal, they are also
accessibly throughout the code in the assembly (Like public, but
access is limited to the assembly and not derived classes).
This is default.
private
protected
internal
58
If the access
modifier is
public
to everyone
private
protected
internal
59
Field Modifiers
Besides the Access Modifiers, a field can have
two additional modifiers
Static (like constant)
The field is part of the class and not the instance of the
class (object). It is accessed by specifying
classname.fieldName and NOT by objectname.fieldName
60
Summary of accessibility
Class can be
accessed by
classes in:
Access Modifiers
Public
Protected
Internal
Private
Another assembly
Yes
N/A
No
N/A
Same assembly
Yes
N/A
Yes
N/A
Containing assembly
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
61
62