Creating Types in C#
Abdulmottaleb Elabour
1
Namespaces
• A namespace is a domain for type names. Types are typically organized into
hierarchical namespaces, making them easier to find and avoiding conflicts.
• Namespaces are independent of assemblies, which are .dll files that serve as units
of deployment
• Namespaces also have no impact on member visibility—public, internal, private, and
so on.
2
Namespaces
The namespace keyword defines a namespace for types within that block; for
example:
The dots in the namespace indicate a hierarchy of nested
namespaces. The code that follows is semantically identical
to the preceding example:
3
The using Directive
The using directive imports a namespace, allowing you to refer to types without their fully
qualified names. The following imports the previous example’s Outer .Middle.Inner
namespace:
The global using Directive
From C# 10, if you prefix a using directive with the global keyword, the directive will
apply to all files in the project or compilation unit.
global using Outer.Middle.Inner;
4
Classes
5
Fields
6
Methods
7
Expression-bodied methods
A method that comprises a single expression, such as
can be written more tersely as an expression-bodied method. A fat arrow replaces the
braces and return keyword:
8
Local methods
You can define a method within another method:
Local methods cannot be overloaded. This means that methods declared in top-level statements
(which are treated as local methods) cannot be overloaded.
9
Instance Constructors
Constructors run initialization code on a class or struct. A constructor is defined
like a method, except that the method name and return type are reduced to the
name of the enclosing type:
10
Access modifiers
public: Code in any assembly can access this type or member. The accessibility level
of the containing type controls the accessibility level of public members of the type.
private: Only code declared in the same class or struct can access this member.
protected: Only code in the same class or in a derived class can access this type or
member.
internal: Only code in the same assembly can access this type or member.
protected internal: Only code in the same assembly or in a derived class in another
assembly can access this type or member.
11
Access modifiers
12
The this Reference
The this reference refers to the instance itself.
13
Inheritance
A class can inherit from another class to extend or customize the original class. Inheriting from a class lets
you reuse the functionality in that class instead of building it from scratch. A class can inherit from only a
single class but can itself be inherited by many classes, thus forming a class hierarchy.
14
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each
other by inheritance.
Inheritance lets us inherit fields and methods from another class. Polymorphism uses those methods to
perform different tasks. This allows us to perform a single action in different ways.
15
Polymorphism
output
this is person class
this is student class
16
Interfaces
An interface is similar to a class, but only specifies behavior and does not hold state
(data). Consequently:
• An interface can define only functions and not fields.
• Interface members are implicitly abstract
• A class (or struct) can implement multiple interfaces. In contrast, a class can inherit from only a single
class, and a struct cannot inherit at all (aside from deriving from System.ValueType).
17
Interfaces
18
Enums
An Enum is a special value type that lets you specify a group of named numeric constants.
For example:
Output:
April
19
Generics
C# has two separate mechanisms for writing code that is reusable across different
types: inheritance and generics. Whereas inheritance expresses reusability with a
base type, generics express reusability with a “template” that contains “placeholder”
types. Generics, when compared to inheritance, can increase type safety and reduce
casting and boxing.
20
Generic Types
A generic type declares type parameters—placeholder types to be filled in by the
consumer of the generic type, which supplies the type arguments. Here is a generic
type Stack<T>, designed to stack instances of type T. Stack<T> declares a single type
parameter T:
21
Why Generics Exist
Generics exist to write code that is reusable across different types. Suppose that
we need a stack of integers but we don’t have generic types. One solution would
be to hardcode a separate version of the class for every required element type
(e.g., IntStack, StringStack, etc.). Clearly, this would cause considerable code
duplication.
22
Generic Methods
A generic method declares type parameters within the signature of a method.
With generic methods, many fundamental algorithms can be implemented in a
general-purpose way. Here is a generic method that swaps the contents of two
variables of any type T:
Output:
Before Swap: a = 5, b = 10
After Swap: a = 10, b = 5 23
Generic Class
Output
42
Ahmed, Mohamed!
24
Read-only and Constant variables
Feature const (Constant) readonly (Read-Only)
Assignment Only at declaration At declaration or in a constructor
Value Type Compile-time constant Runtime constant
Scope Implicitly static Can be instance-level or static
Modification Not allowed after declaration Can be modified in a constructor
Data Type Only primitives and strings Any type (including reference types)
Performance Replaced in compiled code Evaluated at runtime
Best for fixed values (e.g., Pi, Best for values that depend on object
Usage
conversion factors) initialization
25
Delegates
• A delegate is an object that knows how to call a method.
• A delegate type defines the kind of method that delegate instances can call. Specifically,
it defines the method’s return type and its parameter types.
• A Delegate is a Type-Safe Function Pointer. It means the delegate holds the reference of
a method or function and when we invoke the delegate, the method it refers to is going to
be executed. The delegate signature and the method it points to must have the same
signature. While creating the delegate instance, we need to pass the method as a
parameter to the delegate constructor.
26
Delegates
• there are two types of Delegates in C#. They are as follows:
1. Single Cast Delegate: Delegate Refers to a single function or method.
2. Multicast Delegate: Delegate Refers to multiple functions or methods.
27
Single
Cast
Delegate
Output:
Area is 42
28
Perimeter is 26
MultiCast
Delegate
Output:
Area is 42
29
Perimeter is 26
Advantages of delegate
30