Week 2: Getting Dirty – Part 2
-
Upload
jamshid-hashimi -
Category
Software
-
view
57 -
download
2
Transcript of Week 2: Getting Dirty – Part 2
Getting Dirty – Part 2Jamshid Hashimi, CodeWeekend
Agenda
• Structs• Interfaces• Introduction to Delegates and Events• Introduction to Exception Handling• Static Properties & Methods• Using Attributes• Enums• Overloading Operators• Introduction to Generic
Structs
• 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.
Interfaces
• 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 functions.• Delegation is when you request a service by making a method call to
service provider.• 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, event-
driven programs. • 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 in.
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
• Attributes are elements that allow you to add declarative information to your programs.
Enums
• If you have a number of constants that are logically related to each other, then you can group together these constants in an enumeration.• 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.
Overloading Operators
• You can redefine or overload most of the built-in operators available in C#.• Operator overloading provides a much natural abstraction for the
types. • Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator being defined.
Generics
• 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
data type.• Features• It helps you to maximize code reuse, type safety, and performance.• You can create your own generic interfaces, classes, methods, events, and
delegates.• And more..
Source Codes: http://www.facebook.com/groups/CodeWeekend
Thank You!