Week 2: Getting Dirty – Part 2

14
Getting Dirty – Part 2 Jamshid Hashimi, CodeWeekend

Transcript of Week 2: Getting Dirty – Part 2

Page 1: Week 2: Getting Dirty – Part 2

Getting Dirty – Part 2Jamshid Hashimi, CodeWeekend

Page 2: Week 2: Getting Dirty – Part 2

Agenda

• Structs• Interfaces• Introduction to Delegates and Events• Introduction to Exception Handling• Static Properties & Methods• Using Attributes• Enums• Overloading Operators• Introduction to Generic

Page 3: Week 2: Getting Dirty – Part 2

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.

Page 4: Week 2: Getting Dirty – Part 2

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

Page 5: Week 2: Getting Dirty – Part 2

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.

Page 6: Week 2: Getting Dirty – Part 2

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.

Page 7: Week 2: Getting Dirty – Part 2

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.

Page 8: Week 2: Getting Dirty – Part 2

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.

Page 9: Week 2: Getting Dirty – Part 2

Attributes

• Attributes are elements that allow you to add declarative information to your programs.

Page 10: Week 2: Getting Dirty – Part 2

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.

Page 11: Week 2: Getting Dirty – Part 2

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.

Page 12: Week 2: Getting Dirty – Part 2

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..

Page 13: Week 2: Getting Dirty – Part 2

Source Codes: http://www.facebook.com/groups/CodeWeekend

Page 14: Week 2: Getting Dirty – Part 2

Thank You!