In this presentation, the most important object oriented topics and features of C# is discussed. The session was presented in 42nd Session of CodeWeekend and it is the 3rd week of C# + CS50 Series of Training.
Getting Dirty – Part 2
Jamshid Hashimi, CodeWeekend
• Introduction to Delegates and Events
• Introduction to Exception Handling
• Static Properties & Methods
• Using Attributes
• Overloading Operators
• Introduction to Generic
• Struct is a value type
• Classes are reference types
• Structs can’t have destructors, classes do.
• The power of objects lies in their ability to mimic real-world behavior,
through inheritance and polymorphism.
• Supports interface inheritance
• Structs are best used for storing data, but the lack of inheritance and
references can be a serious limitation.
• An interface looks like a class, but has no implementation.
• It contains declarations of:
• Events, methods, properties..
• Inherited by classes and structs
• Interfaces can inherit other interfaces
Delegates and Events
• Delegates are just function pointers, That is, they hold references to
• Delegation is when you request a service by making a method call to
• The service provider then reroutes this service request to another
method, which services the request.
• The delegate class can examine the service request and dynamically
determines at runtime where to route the request.
• Example: A manager receives a service request, she often delegates it
to a member of her department.
Delegates and Events
• Events are variables of type delegates.
• Events are user actions such as key press, clicks, mouse movements,
etc., or some occurrence such as system generated notifcations.
• Applications built with the .NET Framework are object-oriented,
• In Windows Applications, the user interacting with a GUI usually
initiates the event.
• A security class could broadcast an event message when an invalid
login is detected.
• You can subscribe to external events.
Introduction to Exception Handling
• Exceptions are unforeseen errors happen in your programs.
• There are times where you don’t know if an error will happen
• I/O error
• Run out of system memory
• Database and more
• You want to deal with them and this where Exception Handling comes
Static Properties & Methods
• When you declare an object instance of a class, the object
instantiates its own instances of the properties and methods of the
class it implements.
• However, sometimes you may want different object instances of a
class to access the same, shared variables.
• Attributes are elements that allow you to add declarative information
to your programs.
• If you have a number of constants that are logically related to each
other, then you can group together these constants in an
• Enums are strongly typed constants which makes the code more
readable and less prone to errors.
• You can specify another integral numeric type by using a colon.
• Enum allows us to create a new data type.
• You can redefine or overload most of the built-in operators available
• Operator overloading provides a much natural abstraction for the
• Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator being
• Generics allow you to delay the specification of the data type of
programming elements in a class or a method, until it is actually used
in the program
• Generics allow you to write a class or method that can work with any
• It helps you to maximize code reuse, type safety, and performance.
• You can create your own generic interfaces, classes, methods, events, and
• And more..