1 / 40

Tuc Goodwin tgoodwin@ntpcug

Tuc Goodwin tgoodwin@ntpcug.org. Sams Teach Yourself Visual C# 2010 in 24 Hours Hour 3 : Understanding Classes and Object the C# Way. Agenda. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested and Partial Classes

eben
Download Presentation

Tuc Goodwin tgoodwin@ntpcug

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tuc Goodwin tgoodwin@ntpcug.org Sams Teach YourselfVisual C# 2010 in 24 HoursHour 3 : Understanding Classes and Object the C# Way

  2. Agenda • Object and Component-Oriented Programming • Classes in C# • Scope and Accessibility • Methods and Properties • Nested and Partial Classes • Static Classes and Data • Object Initializers

  3. Object and Component-Oriented Programming • A Class is a data structure that combines data storage with methods for manipulating the data. • Four primary OO concepts • Encapsulation • Abstraction • Inheritance • Polymorphism

  4. Classes in C# • Define the body of a class with opening and closing curly braces { } • Scope – Where you declare a variable will determine who can see it. If you can see it, you can use it. • Declaration space – no two entities are allowed to have the same name

  5. Scope and Accessibility

  6. Try It Yourself Demo

  7. Accessibility • Accessibility allows you to control visibility • Namespaces are not allowed to have any access modifiers. They are always public. • Classes default to internal, but are allowed to have either public or internal. • A nested class, a class defined inside of another class defaults to private accessibility • Class members default to private

  8. Access Modifiers

  9. “Black Diamond” • Best practice – Explicitly declaring accessibility indicates the choice was a conscious decision… i.e. self-documenting. • Be careful of “protected internal” because it is effectively one or the other. C# does not provide a concept of protected and internal.

  10. Fields and Constants • Fields are variables that represented data associated with a class. Fields are private by default • Constants are immutable. They can be declared with access modifiers. They must be assigned a value as part of a declaration.

  11. Try It Yourself Demo

  12. Properties • A property provides a simple way to access a field. This allows for encapsulation, hiding the internal details of the field.

  13. Automatic Properties

  14. Declaring a Property

  15. Declaring a Calculated Property

  16. Read-Only and Write-Only Properties • How would you create a read-only property? • Remove the Set method leave only the Get method • How would you create a write-only property? • Remove the Getmethod leave only the Set method A simple mnemonic device: Get – “Gives” Set – “Receives”

  17. Try It Yourself Demo

  18. Methods • Methods (sometimes called functions) define and implement a behavior or action that can be performed.

  19. Methods that returns a value • Methods can accept zero or more declared parameters

  20. Parameters • Value parameters • Reference parameters – uses the ref keyword causes arguments to be passed by reference • Output parameters – uses the out keyword

  21. Parameter Arrays • Parameter arrays are declared with the params keyword • A method’s formal parameter can include only a single parameter array • The parameter array must be the last parameter in the list of any parameter.

  22. Overloaded Methods • …can vary only by signature. • … can vary only by the number and types of parameters • You can overload a method to have different return types, but you should avoid it to minimize the possibility for confusion…

  23. Method Overloading

  24. Try It Yourself Demo

  25. Optional vs. Required Parameters • How do you specify an optional parameter? • A parameter with a default argument is an optional parameter • How do you specify a required parameter? • A parameter without a default argument is a required parameter

  26. Instantiating a Class • You instantiate a class to create an instance Contact c = new Contact(); • A default constructor is the same name as the class and does not return a value.

  27. Declaring a Constructor Overload

  28. How does a class refer to itself? The this keyword

  29. Chaining Constructors

  30. Chaining Constructors

  31. Nested Classes • A nested class is one that is fully enclosed, or nested, inside another class declaration • They have at least the same access level as the containing class.

  32. Partial Classes • Partial classes enable you to split the declaration of a class into multiple parts, typically across multiple files. • Partial classes are implemented in the same way as a normal class but contain the keyword partial.

  33. Static Classes • A static class can have only a static constructor • Static classes can not be instantiated, that is multiple instances cannot be created. • Typically used for utility or helper methods.

  34. Extension Methods • Extension methods must be declared in a non-nested, non-generic static class. • An extension method defined in the same assembly as the type being extended

  35. Try It Yourself Demo

  36. Object Initializers • Suppose you want to instantiate and assign values as part of the constructor call? • This can be done by initializing the object at the same time.

  37. Object Initializers example

  38. Agenda • Object and Component-Oriented Programming • Classes in C# • Scope and Accessibility • Methods and Properties • Nested and Partial Classes • Static Classes and Data • Object Initializers

  39. Questions?

  40. Future Schedule

More Related