C#
Lecture No. 8
Made By:
Dr. Maged Abdullrahman
Namespaces
Dr. Maged Abdullrahman 2
Namespaces
• A namespace may be thought of as a storage area for some classes, which
themselves contain methods.
• Microsoft has written thousands of base classes and stored them in namespaces.
• When we write our C# code, we will create many more classes and we must
follow the same practices as Microsoft and store our classes within our own
namespaces.
• Storing our classes in namespaces makes code more manageable and easier to
maintain.
Dr. Maged Abdullrahman 3
Namespaces
• A namespace can be likened to the folders that we keep our files in.
• We create different folders to hold different files in a structure that best
suits our system.
• Likewise, we can create namespaces to hold our classes, and we can use
the namespaces created by Microsoft in our code to get access to the
Microsoft base classes.
• The lines of code at the start of the program code usually have a
format that starts with the keyword using.
Dr. Maged Abdullrahman 4
Namespaces
• To understand the namespaces, we will go throw two steps:
• See how Microsoft dealing with Namespaces.
• Creating new project in order to create and use Namespace.
Dr. Maged Abdullrahman 5
Namespaces (See how Microsoft dealing with Namespaces)
Dr. Maged Abdullrahman 6
Namespaces (See how Microsoft dealing with Namespaces.)
Dr. Maged Abdullrahman 7
Namespaces (Create new project)
• Notes the following figures:
Dr. Maged Abdullrahman 8
Namespaces (there are 5 using statement, you can omit any directive that not used)
Dr. Maged Abdullrahman 9
Namespaces (right click on the solution and select Add > New project)
Dr. Maged Abdullrahman 10
Namespaces (select Class Library. Name it myProject.EF)
Dr. Maged Abdullrahman 11
Namespaces (Repeat the previous step and create another project.)
• Repeat the previous step and create another project. Name it
myProject.Interface.
• myProject will be as in the following figure:
Dr. Maged Abdullrahman 12
Namespaces (Create a Method inside each class as follows)
Dr. Maged Abdullrahman 13
Namespaces (Create a Method inside each class as follows)
Dr. Maged Abdullrahman 14
Namespaces (Add references to your mainProject)
• In the mainProject, in order to use the methods myMethod() in the
other projects, you must at first include these projects in your
mainProject as follows:
Dr. Maged Abdullrahman 15
Namespaces (Add references to your mainProject)
Dr. Maged Abdullrahman 16
Namespaces (call myMethod() of myProject.EF)
Dr. Maged Abdullrahman 17
Namespaces (call myMethod() of myProject.EF)
Dr. Maged Abdullrahman 18
Namespaces (call myMethod() of myProject.Interface)
Dr. Maged Abdullrahman 19
Namespaces (to correct the previous error, we use Namespace Alias)
Dr. Maged Abdullrahman 20
Inheritance
Dr. Maged Abdullrahman 21
Inheritance
• It is one of the primary pillars of Object Oriented Programming.
• It is a form of software reuse (reduce time and errors) in which a new class is created
by absorbing an existing class’s members and enhancing them with new or modified
capabilities.
• Inheritance lets you save time during app development by reusing proven, high-
performance and debugged high-quality software. This also increases the likelihood that a
system will be implemented effectively.
• The existing class from which a new class inherits members is called the base class, and
the new class is the derived class.
• Suppose that you have two classes as follows in the next slide.
Dr. Maged Abdullrahman 22
Why Inheritance?
Dr. Maged Abdullrahman 23
Why Inheritance?
• From the previous slide, you noticed that there are a lot of code
between these two classes is duplicated.
• So, we will create a base class called Employee and move all the
common code to it, as shown in the next slide.
Dr. Maged Abdullrahman 24
Why Inheritance?
Dr. Maged Abdullrahman 25
Why Inheritance?
Dr. Maged Abdullrahman 26
Inheritance
• In the previous example (Derived Class) inherits from (Base
Class).
• C# supports only single class inheritance.
• C# supports multiple interface inheritance.
• Base classes are automatically instantiated before derived
classes.
• Base class constructor executes before derived class
constructor.
Dr. Maged Abdullrahman 27
Inheritance (Base class constructor executes before derived class constructor)
Dr. Maged Abdullrahman 28
Inheritance (Base class constructor executes before derived class constructor)
Dr. Maged Abdullrahman 29
Inheritance (Base class constructor executes before derived class constructor)
Dr. Maged Abdullrahman 30
Inheritance (Base class constructor executes before derived class constructor)
• From the program output in the previous slide, we notice the
following:
• The base class default constructor execute before the
derived class constructor.
• Only the base class default constructor will execute not
the other constructor.
• But, if you want to execute other constructor of the base
class, you should change the code as illustrated in the
following slide.
Dr. Maged Abdullrahman 31
Inheritance (Calling second constructor of the base class)
Dr. Maged Abdullrahman 32
Method hiding in C#
Dr. Maged Abdullrahman 33
Method hiding in C#
• You can use the new keyword to hide a base class member.
• You will get a compiler warning if you miss the new
keyword.
Dr. Maged Abdullrahman 34
Method hiding in C#
Dr. Maged Abdullrahman 35
Method hiding in C#
Dr. Maged Abdullrahman 36
Method hiding in C#
Dr. Maged Abdullrahman 37
Method hiding in C#
• But, can you invoke a hidden base class member?!!!!
• There are different ways to invoke a hidden base class
member:
1. Using base keyword.
2. Using Cast derived type to base type.
3. Using baseClass reference
Dr. Maged Abdullrahman 38
Invoke a hidden base class member: 1) Using base keyword.
Dr. Maged Abdullrahman 39
Invoke a hidden base class member: 1) Using base keyword.
Dr. Maged Abdullrahman 40
Invoke a hidden base class member: 1) Using base keyword.
Dr. Maged Abdullrahman 41
Invoke a hidden base class member: 2) Using Cast.
Dr. Maged Abdullrahman 42
Invoke a hidden base class member: 2) Using Cast.
Dr. Maged Abdullrahman 43
Invoke a hidden base class member: 2) Using Cast.
Dr. Maged Abdullrahman 44
Invoke a hidden base class member: 3) Using baseClass reference.
Dr. Maged Abdullrahman 45
Invoke a hidden base class member: 3) Using baseClass reference.
Dr. Maged Abdullrahman 46
Invoke a hidden base class member: 3) Using baseClass reference.
Dr. Maged Abdullrahman 47
Homework
• Execute every example in this lecture.
Dr. Maged Abdullrahman 48