tuc goodwin [email protected]. object and component-oriented programming classes in c# scope...
TRANSCRIPT
![Page 1: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/1.jpg)
Sams Teach YourselfVisual C# 2010 in 24 HoursHour 3 : Understanding Classes and Object the C# Way
![Page 2: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/2.jpg)
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
![Page 3: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/3.jpg)
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
![Page 4: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/4.jpg)
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
Class Contact{public int age;
Public void F(){ age = 18;}
public void G(){ int age; age = 21;}
}
![Page 5: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/5.jpg)
Scope and Accessibility
![Page 6: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/6.jpg)
Try It Yourself
Demo
![Page 7: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/7.jpg)
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
![Page 8: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/8.jpg)
Access Modifiers
Modifier Description
public Access is not limited.
protected Access is limited to the containing class or types derived from the containing class.
internal Access is limited to the containing assembly.
protected internal
Access is limited to the containing assembly or types derived from the containing classes.
private Access is limited to the containing class only.
![Page 9: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/9.jpg)
“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.
![Page 10: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/10.jpg)
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.
![Page 11: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/11.jpg)
Try It Yourself
Demo
![Page 12: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/12.jpg)
Properties
A property provides a simple way to access a field. This allows for encapsulation, hiding the internal details of the field.
![Page 13: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/13.jpg)
Automatic Properties
![Page 14: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/14.jpg)
Declaring a Property
![Page 15: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/15.jpg)
Declaring a Calculated Property
![Page 16: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/16.jpg)
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 Get method leave only the Set method
A simple mnemonic device: Get – “Gives” Set –
“Receives”
![Page 17: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/17.jpg)
Try It Yourself
Demo
![Page 18: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/18.jpg)
Methods
Methods (sometimes called functions) define and implement a behavior or action that can be performed.
![Page 19: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/19.jpg)
Methods that returns a value
Methods can accept zero or more declared parameters
![Page 20: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/20.jpg)
Parameters
Value parameters Reference parameters – uses the ref
keyword causes arguments to be passed by reference
Output parameters – uses the out keyword
![Page 21: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/21.jpg)
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.
![Page 22: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/22.jpg)
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…
![Page 23: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/23.jpg)
Method Overloading
![Page 24: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/24.jpg)
Try It Yourself
Demo
![Page 25: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/25.jpg)
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
![Page 26: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/26.jpg)
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.
![Page 27: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/27.jpg)
Declaring a Constructor Overload
![Page 28: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/28.jpg)
How does a class refer to itself?
The this keyword
![Page 29: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/29.jpg)
Chaining Constructors
![Page 30: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/30.jpg)
Chaining Constructors
![Page 31: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/31.jpg)
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.
![Page 32: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/32.jpg)
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.
![Page 33: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/33.jpg)
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.
![Page 34: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/34.jpg)
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
![Page 35: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/35.jpg)
Try It Yourself
Demo
![Page 36: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/36.jpg)
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.
![Page 37: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/37.jpg)
Object Initializers example
![Page 38: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/38.jpg)
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
![Page 39: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/39.jpg)
Questions?
![Page 40: Tuc Goodwin tgoodwin@ntpcug.org. Object and Component-Oriented Programming Classes in C# Scope and Accessibility Methods and Properties Nested](https://reader035.vdocuments.site/reader035/viewer/2022062322/56649eeb5503460f94bfcf2e/html5/thumbnails/40.jpg)
Future Schedule
Chapters Presenter Date
Inheritance, Interfaces, and Abstract Classes David Stark 11/13/2010
Creating Enumerated Types and Structures Shawn Weisfeld 12/11/2010
Events and Event Handling Tuc Goodwin 1/8/2011
Controlling Program Flow David Stark 2/12/2011
Using Stings and Regular Expressions Shawn Weisfeld 3/12/2012
Working with Arrays and Collections Tuc Goodwin 4/9/2011
Handling Errors using Exceptions David Stark 5/14/2011
Understanding Generics Shawn Weisfeld 6/11/2011
Understanding Query Expression Tuc Goodwin 7/9/2011