C#
Lecture No. 7
Made By:
Dr. Maged Abdullrahman
static, instance (Non-static) class members &
static constructor
Dr. Maged Abdullrahman 2
static and instance (Non-static) class members
• When a class member includes a static modifier, the member is called as static
member.
• Static members are invoked using class name (className.staticMember).
• When no static modifier is present the member is called as instance member.
• Instance members are invoked using instances (objects) of the class.
• An instance member belongs to specific instance (object) of a class.
• If I create 3 objects of a class, I will have 3 sets of instance members in the memory. Whereas
there will be only one copy of a static member, no matter how many instances of a class are
created.
Dr. Maged Abdullrahman 3
static and instance (Non-static) class members
• Instance class members
Dr. Maged Abdullrahman 4
static and instance (Non-static) class members
• Instance class members
Dr. Maged Abdullrahman 5
static and instance (Non-static) class members
• Instance class members
Dr. Maged Abdullrahman 6
static and instance (Non-static) class members
• static class member:
• Every object has its own copy of its class’s instance variables.
• In certain cases, only one copy of a particular variable should be shared by all
objects of a class. A static variable (or property) is used in such cases.
• A static variable or property represents classwide information (all objects of
the class share the same piece of data).
• The declaration of a static variable or property begins with the keyword static.
Dr. Maged Abdullrahman 7
static and instance (Non-static) class members
• static Variable Scope
• The scope of a static variable is the body of its class.
• A class’s private static class members can be accessed only through the class’s
methods and properties.
• To access a private static member from outside its class, a public static method or
property can be provided.
• static variables, methods and properties can be used, even if no objects of that class
have been instantiated.
• static members are available as soon as the class is loaded into memory at execution
time.
Dr. Maged Abdullrahman 8
static and instance (Non-static) class members
• static method
• A static method (or property) cannot access non-static class members
directly, because a static method (or property) can be called even when no
objects of the class exist.
• For the same reason, this keyword cannot be used in a static method (the
this reference always refers to a specific object of the class).
• When a static method is called, it does not know which object to manipulate
and there might not be any objects of its class in memory.
Dr. Maged Abdullrahman 9
static and instance (Non-static) class members
• Static class members
Dr. Maged Abdullrahman 10
static and instance (Non-static) class members
• Static class members
Dr. Maged Abdullrahman 11
static and instance (Non-static) class members
• Static class members
Dr. Maged Abdullrahman 12
static constructor
• static constructors are used to initialize static fields in a class.
• You can declare a static constructor by using the keyword static in
front of the constructor name.
• Static constructor is dynamically invoked only once, no matter how
many instances you create.
• Static constructors are invoked before instance constructors.
Dr. Maged Abdullrahman 13
static constructor
Static Class member
Instance Class member
Static
Constructor
Instance
Constructor
Dr. Maged Abdullrahman 14
Getter and Setter Methods
Dr. Maged Abdullrahman 15
Getter and Setter Methods
• Marking the class fields public and exposing them to the external world is
bad, as you will not have a control over what gets assigned and returned.
• Problems with Public fields: (referring to our project “StudentProject”)
• ID should always be non negative number.
• Name can not be set to NULL.
• If student name is missing “No Name” should be returned.
• Marks should be read only.
Dr. Maged Abdullrahman 16
Getter and Setter Methods “Student.cs”
Dr. Maged Abdullrahman 17
Getter and Setter Methods “Student.cs”
Dr. Maged Abdullrahman 18
Getter and Setter Methods “Main method”
Dr. Maged Abdullrahman 19
Getter and Setter Methods
• In the previous program we used Getter and Setter Methods to encapsulate
the class fields.
• As a result, we had a better control on what get assigned and returned from
the class fields. This is called Encapsulation.
• Encapsulation is one of the primary pillars of Object Oriented
Programming.
Dr. Maged Abdullrahman 20
Class properties
Dr. Maged Abdullrahman 21
Class properties
• Beside Getter and Setter Methods, C# have the capability of using a class
properties to encapsulate the class fields.
• Some programming languages does not have this capability, so they are
using the Setter and Getter Methods.
• In the next slide, we will rewrite the previous program using the class
properties.
Dr. Maged Abdullrahman 22
Class properties
Dr. Maged Abdullrahman 23
Class properties
Dr. Maged Abdullrahman 24
Class properties
Dr. Maged Abdullrahman 25
Class properties
3
1
Dr. Maged Abdullrahman 26
Class properties (Property Name’s Declaration)
• Explanation of the property public string Name
• the property is public so it can be used by the class’s clients,
• the property’s type is string and
• the property’s name is Name.
• Explanation of the property Name’s get Accessor
• The get accessor performs the same task as method getName (in Getter & Setter example).
• A get accessor begins with the keyword get, and its body is delimited by braces.
• Like method getName, the get accessor’s body contains a return statement that returns the
value of an students’s name instance variable.
• So, the expression s1.Name gets the value of student’s instance variable name.
Dr. Maged Abdullrahman 27
Class properties (Property Name’s Declaration)
• Explanation of the property Name’s set Accessor
• The set accessor begins with the identifier set followed by its body, which is
delimited by braces.
• In Getter & Setter example, method setName declared a parameter Name to receive
the new name to store in an student object (a set accessor uses the keyword value for
the same purpose).
• value is implicitly declared and initialized for you with the value that the client code
assigns to the property.
• So, value is initialized with Name (the string entered by the user).
• Property Name’s set accessor simply assigns value to the instance variable name.
Dr. Maged Abdullrahman 28
Auto-implemented properties
• public string Name { get; set; } // This is auto-implemented property.
• With an auto-implemented property, the C# compiler automatically creates a
hidden private instance variable, and the get and set accessors for getting and
setting that hidden instance variable.
• This enables you to implement the property trivially, which is handy when
you’re first designing a class.
• If you later decide to include other logic in the get or set accessors, you can
simply implement the property and an instance variable using the
techniques shown in Getter & Setter example.
Dr. Maged Abdullrahman 29
UML Class Diagram for Class Account
Dr. Maged Abdullrahman 30
Example “Accounts”: on auto-implemented properties
Dr. Maged Abdullrahman 31
Example “Accounts”: on auto-implemented properties
Dr. Maged Abdullrahman 32
Example “Accounts”: on auto-implemented properties
Dr. Maged Abdullrahman 33
Example “Accounts”: on auto-implemented properties
Dr. Maged Abdullrahman 34
string format specifiers
Dr. Maged Abdullrahman 35
Composition
• A class can have objects of values types or references to objects of other
classes as members.
• This is called composition and is sometimes referred to as a has a
relationship.
Dr. Maged Abdullrahman 36
Example “Employee”: on Composition
Dr. Maged Abdullrahman 37
Example “Employee”: on Composition
Dr. Maged Abdullrahman 38
Example “Employee”: on static and instance class members using class properties
Dr. Maged Abdullrahman 39
Example on static and instance (Non-static) class members using class properties
Dr. Maged Abdullrahman 40
Homework
• Referring to our project:
• Each class:
• Should have at least two overload constructor.
• Should have a destructor.
• Fields should be encapsulated using class properties.
• Should have a proper methods.
• The main menu should be as follows:
Dr. Maged Abdullrahman 41
Homework
• Press 1 to Enter Teachers Information.
• Press 2 to Enter Studies Levels Names.
• Press 3 to Enter Materials Information.
• Press 4 to Enter Students Information.
• Press 5 to Enter Student Grades.
• You should use array for each class to store the information.
Dr. Maged Abdullrahman 42