1 / 47

C#: Introduction for Developers

Neal Stublen nstublen@jccc.edu. C#: Introduction for Developers. Tonight’s Agenda. Indexers Delegates and events Operator overloading Class inheritance Q&A. But first... Some Review. Review. What methods can you use to get data into and out of a form? Form.Tag property

lynnea
Download Presentation

C#: Introduction for Developers

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. Neal Stublen nstublen@jccc.edu C#: Introduction for Developers

  2. Tonight’s Agenda • Indexers • Delegates and events • Operator overloading • Class inheritance • Q&A

  3. But first...Some Review

  4. Review • What methods can you use to get data into and out of a form? • Form.Tag property • Add methods and properties to the form

  5. Review • What issues might make you think twice about using the Form.Tag property? • It’s a generic Object type, so there are no compiler checks on the data type

  6. Review • Using the Debug class to trace program execution • Stepping through class construction • Multiple constructors • Stepping through properties • get and set • Stepping through form construction and initialization • Construction and initialization

  7. Chapter 13Indexers

  8. What’s an indexer? • An array has an indexer… int[]myArray = new int[5]; for (intindex = 0; index < 5; ++index) { myArray[index] = 3; } • The [ ] is the indexer.

  9. What’s an indexer? • An indexer accesses one value by using another value • A special property: • public <type> this[<index_type> <index_value>] • The index_type is often an integer, but can be another data type • Implemented on collection classes

  10. What’s an indexer? • Indexers can create classes that act like “virtual arrays” • They have the array syntax, but they are not actually allocated as arrays • A special class property: • public <type> this[<index_type> <value>] • The index_type is often an integer, but can be another data type

  11. Invalid Indexes • Using an invalid index value throws an ArgumentException • ArgumentNullException • ArgumentOutOfRangeException

  12. Files as Byte Arrays class FileArray { public byte this[long index] { get { // read byte at index } set { // write byte value at index } } }

  13. Books as Page Arrays class Book { public Page this[long index] { get { // return page “index” } set { // insert page at “index” } } }

  14. Chapter 13, Part 2Delegates and Events

  15. What’s a delegate? • A delegate specifes a method signature for an event • Declare a delegate data type: • public delegate <return_type> DelegateName([parameters_list]);

  16. Delegate Example // The delegate type declaration public delegate void NameChangedEventHandler(object sender, EventArgs e); class NameObject { // The event member public event NameChangedEventHandlerNameChanged; public void setName(string inName) { mName = inName; if (NameChanged != null) { NameChanged(this, new EventArgs()); } } }

  17. Connecting to an Event class SomeObject { private NameObjectmTheName = new NameObject(); public void Init() { mTheName.NameChanged += new NameChangedEventHandler(EventHandler) } private void EventHandler(object sender, EventArgs e) { } }

  18. An Event Example • Reuse the SportsTeam class • One form updates a team’s record • Another form reports the team’s record

  19. Chapter 13, Part 3Operator Overloading

  20. What operations? • Unary operators • +, -, !, ++, --, true, false • public static <return_type> operator <operator>(<operand_type>) • Binary operators • +, -, *, /, %, &, |, ==, !=, >, <, >=, <= • public static <return_type> operator <operator>(<operand_type_1>, <operand_type_2>)

  21. Operator Example class MyCollection { private List<object> mList; public static MyCollection operator + (MyCollection col, object newObject) { col.mList.Add(newObject); return col; } } collection += new object();

  22. Overloading == • Must also overload Equals() • Must also overload GetHashCode() • Must also overload != • Must overload relational operators in pairs (<, >), (<=, >=), (==, !=)

  23. Practice Exercise • Exercise 13-1, p. 418

  24. Chapter 14Class Inheritance

  25. What is inheritance? • One class inherits the attributes and behaviors of another class • The base class should be a larger classification of the derived class (ex. a Control is a larger classification for Button) • A Button “is-a” Control; a Button “has-a” BackColor • A Book “is-a” Product; a Book “has-a” Publisher • The derived class extends or overrides behavior

  26. .NET Inheritance • All classes implicitly inherit from System.Object • GetType() • ToString() • Equals() • ReferenceEquals() • GetHashCode() • Finalize() • MemberwiseClone()

  27. How does inheritance work? Fourth fourth = new Fourth(); fourth.methodA(); fourth.methodB(); fourth.methodC(); fourth.methodD(); fourth.methodE(); First first = new Fourth(); first.methodA(); first.methodB(); first.methodC(); first.methodD(); first.methodE(); “Polymorphism” The base class can take many different forms.

  28. Polymorphism List<First> myList = new List<First>(); myList.Add(new First()); myList.Add(new Second()); myList.Add(new Third()); myList.Add(new Fourth()); foreach (First item in myList) { item.methodB(); item.methodC(); }

  29. How do we “do” inheritance? • Keywords: virtual, override • A base class declares a method as virtual • A derived class declares a method as override • Reference base class from a derived class using “base.”

  30. Inherited Classes class First { public virtual void methodA() { } } class Second : First { public override void methodA() { base.methodA(); } }

  31. Public vs. Private • “public” indicates a method or property can be accessed from outside a class • “private” indicates a method or property can be accessed only from within a class • “protected” indicates a method or property can be accessed from within a class or a derived class

  32. Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { } } class YourClass { public void Sample() { MyClass test; // Access from anywhere test.MyPublicMethod(); } }

  33. Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { } } class MyDerivedClass : MyClass { public void Sample() { // Access from a derived class MyProtectedMethod(); } }

  34. Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { // Access from within the class MyPrivateMethod(); } }

  35. What about “internal”? • The internal keyword provides access to methods and properties, but only from within other files in the same .NET assembly

  36. Casting Operations • A derived class type can be implicitly cast to any of its base class types • Base class types must be explicitly cast to a derived class type

  37. Implicit Casting class Base { } class Derived : Base { } intDoSomething(Base inObject) { } Derived derivedObject = new Derived(); // Implicitly cast to Base class type DoSomething(derivedObject);

  38. Explicit Casting class Base { } class Derived : Base { } intDoSomething(Derived inObject) { } Base someObject = new Derived(); // Explicit cast to Derived class type DoSomething((Derived)someObject); // May throw an exception

  39. Explicit Casting class Base { } class Derived : Base { } intDoSomething(Derived inObject) { } Base someObject = new Derived(); // Explicit cast to Derived class type DoSomething(someObjectas Derived); // May return null

  40. Considerations for Inheritance • Confirm “is-a” versus “has-a” relationship • Does adding one or more properties to the base class make more sense? • Would an interface be more beneficial? • Implicit and explicit casting between inherited types • Using the “as” operator instead of casting to avoid exceptions

  41. Inheritance Example • Example product heirarchy • p. 429

  42. Abstract Classes • Abstract classes cannot be instantiated, and can only serve as a base class • Abstract methods and properties must be overridden in a derived class • You know the method or property exists for every object of this type, but there is no implementation at this level of abstraction • All Objects have a ToString() implementation, but the implementations are all independent of one another

  43. Sealed Classes • Sealed classes cannot be inherited • Sealed methods and properties cannot be overridden

  44. Start thinking about how objects you need to model may inherit from one another.Are there any obvious heirarchies, common attributes, or shared behavior?

  45. Suggestions • Try to work through Exercises 14-1 and 14-2 in the book (p. 457)

  46. Inheritance Walkthrough

More Related