Advanced C # Extracted from (with very minor modifications) Mark Sapossnek CS 594 Computer Science Department Metropolitan College Boston University Find the original set on WWW.GotDotNet.com Jim  Fawcett CSE 681 – Software Modeling & Analysis
Learning Objectives Advanced features of the C# language Creating custom types with interfaces, classes  and structs Delegates and events Miscellaneous topics
Agenda Review Object-Oriented Concepts Interfaces Classes and Structs Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code
Objects, instances and classes Identity Every instance has a unique identity, regardless of  its data Encapsulation Data and function are packaged together Information hiding An object is an abstraction User should NOT know implementation details Review  Key Object-Oriented Concepts
Review  Key Object-Oriented Concepts Interfaces A well-defined contract A set of function members Types An object has a type, which specifies its interfaces and their implementations A variable also can have a type Inheritance Types are arranged in a hierarchy Base/derived, superclass/subclass Interface vs. implementation inheritance
Review  Key Object-Oriented Concepts Polymorphism The ability to use an object without knowing its precise type Three main kinds of polymorphism Inheritance Interfaces Late binding Dependencies For reuse and to facilitate development, systems should be loosely coupled Dependencies should be minimized
Interfaces An interface defines a contract An interface is a type Includes methods, properties, indexers, events Any class or struct implementing an interface must support all parts of the contract Interfaces provide no implementation When a class or struct implements an interface it must provide the implementation Interfaces provide polymorphism Many classes and structs may implement  a particular interface
Interfaces Example public interface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() { ... } } public class Car : IDelete { public void Delete() { ... } } TextBox tb = new TextBox(); IDelete iDel = tb; iDel.Delete(); Car c = new Car(); iDel = c; iDel.Delete();
Interfaces Multiple Inheritance Classes and structs can inherit from  multiple interfaces Interfaces can inherit from multiple interfaces interface IControl {   void Paint(); } interface IListBox: IControl {   void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { }
Interfaces Explicit Interface Members If two interfaces have the same method name, you can explicitly specify interface + method name to disambiguate their implementations interface IControl {   void Delete(); } interface IListBox: IControl {   void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); }
Classes and Structs Similarities Both are user-defined types Both can implement multiple interfaces Both can contain Data  Fields, constants, events, arrays Functions  Methods, properties, indexers, operators, constructors Type definitions Classes, structs, enums, interfaces, delegates
Classes and Structs Differences No user-defined parameterless constructor Can have user-defined parameterless constructor No destructor Can have a destructor No inheritance (inherits only from  System.ValueType ) Can inherit from any  non-sealed reference type Value type Reference type Struct Class
Classes   and Structs C# Structs vs. C++ Structs Very different from C++ struct Members can be  public ,  internal  or  private Members are always  public Always allocated on the stack or  as a member Can be allocated on the heap,  on the stack or as a member  (can be used as value or reference) User-defined value type Same as C++ class, but all members are  public C# Struct C++ Struct
Classes and Structs Class public class Car : Vehicle { public enum Make { GM, Honda, BMW } Make make; string vid; Point location; Car(Make m, string vid; Point loc) {  this.make = m;  this.vid = vid; this.location = loc;  } public void Drive() {    Console.WriteLine(ā€œvroomā€); } } Car c = new Car(Car.Make.BMW, ā€œ JF3559QT98ā€,    new Point(3,7)); c.Drive();
Classes and Structs Struct public struct Point  { int x, y; public Point(int x, int y) { this.x = x;  this.y = y; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point p = new Point(2,5); p.X += 100; int px = p.X;  // px = 102
Classes   and Structs Static vs. Instance Members By default, members are per instance Each instance gets its own fields Methods apply to a specific instance Static members are per type Static methods can’t access instance data No  this  variable in static methods Don’t abuse static members They are essentially object-oriented  global data and global functions
Classes and Structs Access Modifiers Access modifiers specify who can use a type or a member Access modifiers control encapsulation Top-level types (those directly in a namespace) can be  public  or  internal  Class members can be  public ,  private ,  protected ,  internal , or   protected internal Struct members can be  public ,  private  or  internal
Classes and Structs Access Modifiers to T or types derived from T protected within T only (the default) private to T or types derived from T or to types within A protected internal to types within A internal to everyone public Then a member defined in type T and assembly A is accessible If the access  modifier is
Classes and Structs Abstract Classes An abstract class is one that cannot  be instantiated Intended to be used as a base class May contain abstract and non-abstract  function members Similar to an interface Cannot be sealed
Classes and Structs Sealed Classes A sealed class is one that cannot be used as a base class Sealed classes can’t be abstract All structs are implicitly sealed Why seal a class? To prevent unintended derivation Code optimization Virtual function calls can be resolved at compile-time
Classes and Structs this The  this  keyword is a predefined variable available in non-static function members  Used to access data and function members unambiguously class Person { string name; public Person(string name) { this.name = name; } public void Introduce(Person p) { if (p != this) Console.WriteLine(ā€œHi, I’m ā€œ + name); } }
Classes and Structs base The  base  keyword is used to access class members that are hidden by similarly named members of the current class class Shape { int x, y; public override string ToString() { return "x=" + x + ",y=" + y; } } class Circle : Shape { int r; public override string ToString() { return base.ToString() + ",r=" + r; } }
Classes and Structs Constants A constant is a data member that is evaluated at compile-time and is implicitly static (per type) e.g.  Math.PI public class MyClass { public const string version = ā€œ1.0.0ā€; public const string s1 = ā€œabcā€ + ā€œdefā€; public const int i3 = 1 + 2; public const double PI_I3 = i3 * Math.PI; public const double s = Math.Sin(Math.PI);  //ERROR ... }
Classes and Structs Fields A field is a member variable Holds data for a class or struct Can hold: a class instance (a reference),  a struct instance (actual data), or  an array of class or struct instances  (an array is actually a reference)
Classes and Structs Readonly Fields Similar to a const, but is initialized at  run-time in its declaration or in a constructor Once initialized, it cannot be modified Differs from a constant  Initialized at run-time (vs. compile-time) Don’t have to re-compile clients Can be static or per-instance public class MyClass { public static readonly double d1 = Math.Sin(Math.PI); public readonly string s1; public MyClass(string s) { s1 = s; }  }
Classes and Structs Properties A property is a virtual field Looks like a field, but is implemented with code public class Button: Control { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); } } } Button b = new Button(); b.Caption = "OK"; String s = b.Caption; Can be  read-only,  write-only,  or read/write
Classes and Structs Indexers An indexer lets an instance behave as a  virtual array Can be overloaded  (e.g. index by  int  and by  string ) public class ListBox: Control { private string[] items; public string this[int index] { get { return items[index]; } set { items[index] = value; Repaint(); } } } ListBox listBox = new ListBox(); listBox[0] = "hello"; Console.WriteLine(listBox[0]); Can be  read-only,  write-only,  or read/write
Classes and Structs Methods All code executes in a method Constructors, destructors and operators are special types of methods Properties and indexers are implemented with  get/set methods Methods have argument lists Methods contain statements Methods can return a value Only if return type is not  void
Classes and Structs Method Argument Passing By default, data is passed by value A copy of the data is created and passed  to the method For value types, variables cannot be modified by a method call For reference types, the instance can be modified by a method call, but the variable itself cannot be modified by a method call
Classes and Structs Method Argument Passing The  ref  modifier causes arguments to be passed by reference Allows a method call to modify a variable Have to use  ref  modifier in method definition and the code that calls it Variable has to have a value before call void RefFunction(ref int p) { p++; } int x = 10; RefFunction(ref x); // x is now 11
Classes and Structs Method Argument Passing The  out  modifier causes arguments to be passed out by reference Allows a method call to initialize a variable Have to use  out  modifier in method definition and the code that calls it Argument has to have a value before returning void OutFunction(out int p) { p = 22; } int x; OutFunction(out x); // x is now 22
Classes and Structs Overloaded Methods A type may overload methods, i.e. provide multiple methods with the same name Each must have a unique signature Signature is based upon arguments only, the return value is ignored void Print(int i); void Print(string s); void Print(char c); void Print(float f); int Print(float f);  // Error: duplicate signature
Classes and Structs Parameter Arrays Methods can have a variable number of arguments, called a parameter array params  keyword declares parameter array Must be last argument int Sum(params int[] intArr) { int sum = 0; foreach (int i in intArr) sum += i; return sum; } int sum = Sum(13,87,34);
Classes and Structs Virtual Methods Methods may be virtual or non-virtual (default) Non-virtual methods are not polymorphic They cannot be overridden Non-virtual methods cannot be abstract class Foo { public void DoSomething(int i) { ... } } Foo f = new Foo(); f.DoSomething();
Classes and Structs Virtual Methods Defined in a base class Can be overridden in derived classes Derived classes provide their own specialized implementation May contain a default implementation Use abstract method if no default implementation A form of polymorphism Properties, indexers and events can also  be virtual
Classes and Structs Virtual Methods class Shape { public virtual void Draw() { ... } } class Box : Shape {  public override void Draw() { ... } } class Sphere : Shape {  public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... } HandleShape(new Box()); HandleShape(new Sphere()); HandleShape(new Shape());
Classes and Structs Abstract Methods An abstract method is virtual and has no implementation Must belong to an abstract class Intended to be implemented in a derived class
Classes and Structs Abstract Methods abstract class Shape { public abstract void Draw(); } class Box : Shape {  public override void Draw() { ... } } class Sphere : Shape {  public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... } HandleShape(new Box()); HandleShape(new Sphere()); HandleShape(new Shape()); // Error!
Classes and Structs Method Versioning Must explicitly use  override  or  new  keywords to specify versioning intent Avoids accidental overriding Methods are non-virtual by default C++ and Java product fragile base classes – cannot specify versioning intent
Classes and Structs Method Versioning class Derived: Base {   // version 1 public virtual void Foo() { Console.WriteLine("Derived.Foo");  } } class Base {  // version 1 } class Base {    // version 2  public virtual void Foo() { Console.WriteLine("Base.Foo");  } } class Derived: Base {  // version 2a new public virtual void Foo() { Console.WriteLine("Derived.Foo");  } } class Derived: Base {   // version 2b public override void Foo() { base.Foo(); Console.WriteLine("Derived.Foo");  } }
Classes and Structs Constructors Instance constructors are special methods that are called when a class or struct is instantiated Performs custom initialization Can be overloaded If a class doesn’t define any constructors, an implicit parameterless constructor is created Cannot create a parameterless constructor for  a struct All fields initialized to zero/null
Classes and Structs Constructor Initializers One constructor can call another with a constructor initializer Can call  this(...)  or  base(...) Default constructor initializer is  base() class B { private int h; public B() { } public B(int h) { this.h = h; } } class D : B { private int i; public D() : this(24) { } public D(int i) { this.i = i; } public D(int h, int i) : base(h) { this.i = i; } }
Classes and Structs Static Constructors A static constructor lets you create initialization code that is called once for the class Guaranteed to be executed before the first instance of a class or struct is created and before any static member of the class or struct is accessed No other guarantees on execution order Only one static constructor per type Must be parameterless
Classes and Structs Destructors A destructor is a method that is called before an instance is garbage collected Used to clean up any resources held by the instance, do bookkeeping, etc.  Only classes, not structs can have destructors class Foo { ~Foo() {  Console.WriteLine(ā€œDestroyed {0}ā€, this);  } }
Classes and Structs Destructors Unlike C++, C# destructors are non-deterministic They are not guaranteed to be called at a specific time They are guaranteed to be called before shutdown Use the  using  statement and the  IDisposable  interface to achieve deterministic finalization
Classes and Structs Operator Overloading User-defined operators Must be a static method class Car { string vid; public static bool operator ==(Car x, Car y) { return x.vid == y.vid; } }
Classes and Structs Operator Overloading Overloadable unary operators Overloadable binary operators -- ++ false true ~ ! - + >= != ~ <= > < >> << == ^ | & % ! / * - +
Classes and Structs Operator Overloading No overloading for member access, method invocation, assignment operators, nor these operators:  sizeof ,  new ,  is ,  as ,  typeof ,  checked ,  unchecked ,  && ,  || , and  ?: The  &&  and  ||  operators are automatically evaluated from  &  and  | Overloading a binary operator (e.g.  * ) implicitly overloads the corresponding assignment operator (e.g.  *= )
Classes and Structs Operator Overloading struct Vector { int x, y; public Vector(x, y) { this.x = x; this.y = y; } public static Vector operator +(Vector a, Vector b) { return Vector(a.x + b.x, a.y + b.y); } ... }
Classes and Structs Conversion Operators User-defined explicit and implicit conversions class Note { int value; // Convert to hertz – no loss of precision public static implicit operator double(Note x) { return ...; } // Convert to nearest note public static explicit operator Note(double x) { return ...; } } Note n = (Note)442.578; double d = n;
Classes and Structs Implementing Interfaces Classes and structs can implement multiple interfaces A class or struct that inherits from an interface must implement all function members defined in that interface
Classes and Structs Implementing Interfaces public interface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() { ... } } public class Car : IDelete { public void Delete() { ... } } TextBox tb = new TextBox(); IDelete iDel = tb; iDel.Delete(); Car c = new Car(); iDel = c; iDel.Delete();
Classes and Structs Implementing Interfaces Explicit interface implementation Handles name collisions public interface IDelete { void Delete(); } public interface IFoo { void Delete(); } public class TextBox : IDelete, IFoo { public void IDelete.Delete() { ... } public void IFoo.Delete() { ... } }
Classes and Structs Nested Types Declared within the scope of another type Nesting a type provides three benefits: Nested type can access all the members of its enclosing type, regardless of access modifer Nested type can be hidden from other types Accessing a nested type from outside the enclosing type requires specifying the type name Nested types can be declared  new  to hide inherited types Unlike Java inner classes, nested types imply no relationship between instances
Classes and Structs is  Operator The  is  operator is used to dynamically test if the run-time type of an object is compatible with a given type static void DoSomething(object o) { if (o is Car)  ((Car)o).Drive(); } Don’t abuse the  is  operator: it is preferable to design an appropriate type hierarchy with polymorphic methods
Classes and Structs as  Operator The  as  operator tries to convert a variable to a specified type; if no such conversion is possible the result is  null static void DoSomething(object o) { Car c = o as Car; if (c != null) c.Drive(); } More efficient than using  is  operator: test and convert in one operation Same design warning as with the  is  operator
Classes and Structs typeof  Operator The  typeof  operator returns the  System.Type  object for a specified type Can then use reflection to dynamically obtain information about the type Console.WriteLine(typeof(int).FullName); Console.WriteLine(typeof(System.Int).Name); Console.WriteLine(typeof(float).Module); Console.WriteLine(typeof(double).IsPublic); Console.WriteLine(typeof(Car).MemberType);
Delegates Overview A delegate is a reference type that defines a method signature A delegate instance holds one or more methods Essentially an ā€œobject-oriented function pointerā€ Methods can be static or non-static Methods can return a value Provides polymorphism for individual functions Foundation for event handling
Delegates Overview delegate double Del(double x);  // Declare static void DemoDelegates() { Del delInst = new Del(Math.Sin);  // Instantiate double x = delInst(1.0);  // Invoke }
Delegates Multicast Delegates A delegate can hold and invoke multiple methods Multicast delegates must contain only methods that return  void , else there is a run-time exception Each delegate has an invocation list Methods are invoked sequentially, in the order added The  +=  and  -=  operators are used to add and remove delegates, respectively +=  and  -=  operators are thread-safe
Delegates Multicast Delegates delegate void SomeEvent(int x, int y); static void Foo1(int x, int y) { Console.WriteLine(&quot;Foo1&quot;); } static void Foo2(int x, int y) { Console.WriteLine(&quot;Foo2&quot;); } public static void Main() { SomeEvent func = new SomeEvent(Foo1); func += new SomeEvent(Foo2); func(1,2);  // Foo1 and Foo2 are called func -= new SomeEvent(Foo1); func(2,3);  // Only Foo2 is called }
Delegates and Interfaces Could always use interfaces instead of delegates Interfaces are more powerful Multiple methods Inheritance Delegates are more elegant for event handlers Less code Can easily implement multiple event handlers on one class/struct
Events Overview Event handling is a style of programming where one object notifies another that something of interest has occurred A publish-subscribe programming model Events allow you to tie your own code into the functioning of an independently created component Events are a type of ā€œcallbackā€ mechanism
Events Overview Events are well suited for user-interfaces The user does something (clicks a button, moves a mouse, changes a value, etc.) and the program reacts in response Many other uses, e.g. Time-based events Asynchronous operation completed Email message has arrived A web session has begun
Events Overview C# has native support for events Based upon delegates An event is essentially a field holding a delegate However, public users of the class can only register delegates They can only call  +=  and  -= They can’t invoke the event’s delegate Multicast delegates allow multiple objects to register with the same event
Events Example: Component-Side Define the event signature as a delegate Define the event and firing logic public delegate void EventHandler(object sender,  EventArgs e); public class Button {   public event EventHandler Click; protected void OnClick(EventArgs e) { // This is called when button is clicked if (Click != null) Click(this, e);   } }
Events Example: User-Side Define and register an event handler public class MyForm: Form { Button okButton; static void OkClicked(object sender, EventArgs e) { ShowMessage(&quot;You pressed the OK button&quot;); } public MyForm() { okButton = new Button(...); okButton.Caption = &quot;OK&quot;; okButton.Click += new EventHandler(OkClicked); } }
Attributes Overview It’s often necessary to associate information (metadata) with types and members, e.g. Documentation URL for a class Transaction context for a method XML persistence mapping COM ProgID for a class Attributes allow you to decorate a code element (assembly, module, type, member, return value and parameter) with additional information
Attributes Overview [HelpUrl(ā€œhttp://SomeUrl/APIDocs/SomeClassā€)] class SomeClass { [Obsolete(ā€œUse SomeNewMethod insteadā€)] public void SomeOldMethod() {  ...  } public string Test([SomeAttr()] string param1) { ...  } }
Attributes Overview Attributes are superior to the alternatives Modifying the source language Using external files, e.g., .IDL, .DEF Attributes are extensible Attributes allow to you add information not supported by C# itself Not limited to predefined information Built into the .NET Framework, so they work across all .NET languages Stored in assembly metadata
Attributes Overview Some predefined .NET Framework attributes COM Prog ID ProgId Transactional characteristics of a class Transaction Compiler will complain if target is used Obsolete Allows a class or struct to be serialized Serializable Should a property or event be displayed in the property window Browsable Description Attribute Name
Attributes Overview Attributes can be Attached to types and members Examined at run-time using reflection Completely extensible Simply a class that inherits from  System.Attribute Type-safe Arguments checked at compile-time Extensive use in .NET Framework XML, Web Services, security, serialization,  component model, COM and P/Invoke interop,  code configuration…
Attributes Querying Attributes [HelpUrl(&quot;http://SomeUrl/MyClass&quot;)]  class Class1 {} [HelpUrl(&quot;http://SomeUrl/MyClass&quot;), HelpUrl(&quot;http://SomeUrl/MyClassā€, Tag=ā€œctorā€)]  class Class2 {} Type type = typeof(MyClass);  foreach (object attr in type.GetCustomAttributes() ) {  if ( attr is HelpUrlAttribute )  { HelpUrlAttribute ha = (HelpUrlAttribute) attr; myBrowser.Navigate( ha.Url ); } }
C# provides preprocessor directives that serve a number of functions Unlike C++, there is not a separate preprocessor The ā€œpreprocessorā€ name is preserved only for consistency with C++ C++ preprocessor features removed include: #include : Not really needed with one-stop programming; removal results in faster compilation Macro version of  #define : removed for clarity Preprocessor Directives Overview
Preprocessor Directives Overview Delimit outline regions #region ,  #end Specify line number #line Issue errors and warnings #error ,  #warning Conditionally skip sections of code #if,   #elif ,  #else ,  #endif Define and undefine conditional symbols #define ,  #undef Description Directive
Preprocessor Directives Conditional Compilation #define Debug public class Debug { [Conditional(&quot;Debug&quot;)] public static void Assert(bool cond, String s) { if (!cond) { throw new AssertionException(s); } } void DoSomething() { ... // If Debug is not defined, the next line is // not even called Assert((x == y), ā€œX should equal Yā€); ... } }
Preprocessor Directives Assertions By the way, assertions are an incredible way to improve the quality of your code An assertion is essentially a unit test built right into your code You should have assertions to test preconditions, postconditions and invariants Assertions are only enabled in debug builds Your code is QA’d every time it runs Must read: ā€œ Writing Solid Code ā€, by Steve Maguire, Microsoft Press, ISBN 1-55615-551-4
XML Comments Overview Programmers don’t like to document code, so we need a way to make it easy for them to produce quality,  up-to-date documentation C# lets you embed XML comments that document types, members, parameters, etc. Denoted with triple slash:  /// XML document is generated when code is compiled with  /doc  argument Comes with predefined XML schema, but you can add your own tags too Some are verified, e.g. parameters, exceptions, types
XML Comments Overview Formatting hints <list>, <item>, ... Use of a parameter <paramref> Cross references <see>, <seealso> Sample code <example>, <c>, <code> Property <value> Exceptions thrown from method <exception> Permission requirements <permission> Method return value <returns> Method parameter <param> Type or member <summary>, <remarks> Description XML Tag
XML Comments Overview class XmlElement { /// <summary> ///  Returns the attribute with the given name and ///  namespace</summary> /// <param name=&quot;name&quot;> ///  The name of the attribute</param> /// <param name=&quot;ns&quot;> ///  The namespace of the attribute, or null if ///  the attribute has no namespace</param> /// <return> ///  The attribute value, or null if the attribute ///  does not exist</return> /// <seealso cref=&quot;GetAttr(string)&quot;/> /// public string GetAttr(string name, string ns) { ... } }
Unsafe Code Overview Developers sometime need total control Performance extremes Dealing with existing binary structures Existing code Advanced COM support, DLL import C# allows you to mark code as unsafe, allowing Pointer types, pointer arithmetic  -> ,  *  operators Unsafe casts No garbage collection
Unsafe Code Overview Lets you embed native C/C++ code Basically ā€œinline Cā€ Must ensure the GC doesn’t move your data Use  fixed  statement to pin data Use  stackalloc  operator so memory is allocated on stack, and need not be pinned unsafe void Foo() { char* buf = stackalloc char[256]; for (char* p = buf; p < buf + 256; p++) *p = 0; ... }
Unsafe Code Overview class FileStream: Stream { int handle; public unsafe int Read(byte[] buffer, int index,  int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, null); } return n; } [dllimport(&quot;kernel32&quot;, SetLastError=true)] static extern unsafe bool ReadFile(int hFile, void* lpBuffer, int nBytesToRead, int* nBytesRead, Overlapped* lpOverlapped); }
Unsafe Code C# and Pointers Power comes at a price! Unsafe means unverifiable code Stricter security requirements  Before the code can run Downloading code
More Resources http://msdn.microsoft.com http://windows.oreilly.com/news/hejlsberg_0800.html http://www.csharphelp.com/ http://www.csharp-station.com/ http://www.csharpindex.com/ http://msdn.microsoft.com/msdnmag/issues/0900/csharp/csharp.asp http://www.hitmill.com/programming/dotNET/csharp.html http://www.c-sharpcorner.com/ http://msdn.microsoft.com/library/default.asp?URL=/library/dotnet/csspec/vclrfcsharpspec_Start.htm

Advanced c#

  • 1.
    Advanced C #Extracted from (with very minor modifications) Mark Sapossnek CS 594 Computer Science Department Metropolitan College Boston University Find the original set on WWW.GotDotNet.com Jim Fawcett CSE 681 – Software Modeling & Analysis
  • 2.
    Learning Objectives Advancedfeatures of the C# language Creating custom types with interfaces, classes and structs Delegates and events Miscellaneous topics
  • 3.
    Agenda Review Object-OrientedConcepts Interfaces Classes and Structs Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code
  • 4.
    Objects, instances andclasses Identity Every instance has a unique identity, regardless of its data Encapsulation Data and function are packaged together Information hiding An object is an abstraction User should NOT know implementation details Review Key Object-Oriented Concepts
  • 5.
    Review KeyObject-Oriented Concepts Interfaces A well-defined contract A set of function members Types An object has a type, which specifies its interfaces and their implementations A variable also can have a type Inheritance Types are arranged in a hierarchy Base/derived, superclass/subclass Interface vs. implementation inheritance
  • 6.
    Review KeyObject-Oriented Concepts Polymorphism The ability to use an object without knowing its precise type Three main kinds of polymorphism Inheritance Interfaces Late binding Dependencies For reuse and to facilitate development, systems should be loosely coupled Dependencies should be minimized
  • 7.
    Interfaces An interfacedefines a contract An interface is a type Includes methods, properties, indexers, events Any class or struct implementing an interface must support all parts of the contract Interfaces provide no implementation When a class or struct implements an interface it must provide the implementation Interfaces provide polymorphism Many classes and structs may implement a particular interface
  • 8.
    Interfaces Example publicinterface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() { ... } } public class Car : IDelete { public void Delete() { ... } } TextBox tb = new TextBox(); IDelete iDel = tb; iDel.Delete(); Car c = new Car(); iDel = c; iDel.Delete();
  • 9.
    Interfaces Multiple InheritanceClasses and structs can inherit from multiple interfaces Interfaces can inherit from multiple interfaces interface IControl { void Paint(); } interface IListBox: IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { }
  • 10.
    Interfaces Explicit InterfaceMembers If two interfaces have the same method name, you can explicitly specify interface + method name to disambiguate their implementations interface IControl { void Delete(); } interface IListBox: IControl { void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); }
  • 11.
    Classes and StructsSimilarities Both are user-defined types Both can implement multiple interfaces Both can contain Data Fields, constants, events, arrays Functions Methods, properties, indexers, operators, constructors Type definitions Classes, structs, enums, interfaces, delegates
  • 12.
    Classes and StructsDifferences No user-defined parameterless constructor Can have user-defined parameterless constructor No destructor Can have a destructor No inheritance (inherits only from System.ValueType ) Can inherit from any non-sealed reference type Value type Reference type Struct Class
  • 13.
    Classes and Structs C# Structs vs. C++ Structs Very different from C++ struct Members can be public , internal or private Members are always public Always allocated on the stack or as a member Can be allocated on the heap, on the stack or as a member (can be used as value or reference) User-defined value type Same as C++ class, but all members are public C# Struct C++ Struct
  • 14.
    Classes and StructsClass public class Car : Vehicle { public enum Make { GM, Honda, BMW } Make make; string vid; Point location; Car(Make m, string vid; Point loc) { this.make = m; this.vid = vid; this.location = loc; } public void Drive() { Console.WriteLine(ā€œvroomā€); } } Car c = new Car(Car.Make.BMW, ā€œ JF3559QT98ā€, new Point(3,7)); c.Drive();
  • 15.
    Classes and StructsStruct public struct Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point p = new Point(2,5); p.X += 100; int px = p.X; // px = 102
  • 16.
    Classes and Structs Static vs. Instance Members By default, members are per instance Each instance gets its own fields Methods apply to a specific instance Static members are per type Static methods can’t access instance data No this variable in static methods Don’t abuse static members They are essentially object-oriented global data and global functions
  • 17.
    Classes and StructsAccess Modifiers Access modifiers specify who can use a type or a member Access modifiers control encapsulation Top-level types (those directly in a namespace) can be public or internal Class members can be public , private , protected , internal , or protected internal Struct members can be public , private or internal
  • 18.
    Classes and StructsAccess Modifiers to T or types derived from T protected within T only (the default) private to T or types derived from T or to types within A protected internal to types within A internal to everyone public Then a member defined in type T and assembly A is accessible If the access modifier is
  • 19.
    Classes and StructsAbstract Classes An abstract class is one that cannot be instantiated Intended to be used as a base class May contain abstract and non-abstract function members Similar to an interface Cannot be sealed
  • 20.
    Classes and StructsSealed Classes A sealed class is one that cannot be used as a base class Sealed classes can’t be abstract All structs are implicitly sealed Why seal a class? To prevent unintended derivation Code optimization Virtual function calls can be resolved at compile-time
  • 21.
    Classes and Structsthis The this keyword is a predefined variable available in non-static function members Used to access data and function members unambiguously class Person { string name; public Person(string name) { this.name = name; } public void Introduce(Person p) { if (p != this) Console.WriteLine(ā€œHi, I’m ā€œ + name); } }
  • 22.
    Classes and Structsbase The base keyword is used to access class members that are hidden by similarly named members of the current class class Shape { int x, y; public override string ToString() { return &quot;x=&quot; + x + &quot;,y=&quot; + y; } } class Circle : Shape { int r; public override string ToString() { return base.ToString() + &quot;,r=&quot; + r; } }
  • 23.
    Classes and StructsConstants A constant is a data member that is evaluated at compile-time and is implicitly static (per type) e.g. Math.PI public class MyClass { public const string version = ā€œ1.0.0ā€; public const string s1 = ā€œabcā€ + ā€œdefā€; public const int i3 = 1 + 2; public const double PI_I3 = i3 * Math.PI; public const double s = Math.Sin(Math.PI); //ERROR ... }
  • 24.
    Classes and StructsFields A field is a member variable Holds data for a class or struct Can hold: a class instance (a reference), a struct instance (actual data), or an array of class or struct instances (an array is actually a reference)
  • 25.
    Classes and StructsReadonly Fields Similar to a const, but is initialized at run-time in its declaration or in a constructor Once initialized, it cannot be modified Differs from a constant Initialized at run-time (vs. compile-time) Don’t have to re-compile clients Can be static or per-instance public class MyClass { public static readonly double d1 = Math.Sin(Math.PI); public readonly string s1; public MyClass(string s) { s1 = s; } }
  • 26.
    Classes and StructsProperties A property is a virtual field Looks like a field, but is implemented with code public class Button: Control { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); } } } Button b = new Button(); b.Caption = &quot;OK&quot;; String s = b.Caption; Can be read-only, write-only, or read/write
  • 27.
    Classes and StructsIndexers An indexer lets an instance behave as a virtual array Can be overloaded (e.g. index by int and by string ) public class ListBox: Control { private string[] items; public string this[int index] { get { return items[index]; } set { items[index] = value; Repaint(); } } } ListBox listBox = new ListBox(); listBox[0] = &quot;hello&quot;; Console.WriteLine(listBox[0]); Can be read-only, write-only, or read/write
  • 28.
    Classes and StructsMethods All code executes in a method Constructors, destructors and operators are special types of methods Properties and indexers are implemented with get/set methods Methods have argument lists Methods contain statements Methods can return a value Only if return type is not void
  • 29.
    Classes and StructsMethod Argument Passing By default, data is passed by value A copy of the data is created and passed to the method For value types, variables cannot be modified by a method call For reference types, the instance can be modified by a method call, but the variable itself cannot be modified by a method call
  • 30.
    Classes and StructsMethod Argument Passing The ref modifier causes arguments to be passed by reference Allows a method call to modify a variable Have to use ref modifier in method definition and the code that calls it Variable has to have a value before call void RefFunction(ref int p) { p++; } int x = 10; RefFunction(ref x); // x is now 11
  • 31.
    Classes and StructsMethod Argument Passing The out modifier causes arguments to be passed out by reference Allows a method call to initialize a variable Have to use out modifier in method definition and the code that calls it Argument has to have a value before returning void OutFunction(out int p) { p = 22; } int x; OutFunction(out x); // x is now 22
  • 32.
    Classes and StructsOverloaded Methods A type may overload methods, i.e. provide multiple methods with the same name Each must have a unique signature Signature is based upon arguments only, the return value is ignored void Print(int i); void Print(string s); void Print(char c); void Print(float f); int Print(float f); // Error: duplicate signature
  • 33.
    Classes and StructsParameter Arrays Methods can have a variable number of arguments, called a parameter array params keyword declares parameter array Must be last argument int Sum(params int[] intArr) { int sum = 0; foreach (int i in intArr) sum += i; return sum; } int sum = Sum(13,87,34);
  • 34.
    Classes and StructsVirtual Methods Methods may be virtual or non-virtual (default) Non-virtual methods are not polymorphic They cannot be overridden Non-virtual methods cannot be abstract class Foo { public void DoSomething(int i) { ... } } Foo f = new Foo(); f.DoSomething();
  • 35.
    Classes and StructsVirtual Methods Defined in a base class Can be overridden in derived classes Derived classes provide their own specialized implementation May contain a default implementation Use abstract method if no default implementation A form of polymorphism Properties, indexers and events can also be virtual
  • 36.
    Classes and StructsVirtual Methods class Shape { public virtual void Draw() { ... } } class Box : Shape { public override void Draw() { ... } } class Sphere : Shape { public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... } HandleShape(new Box()); HandleShape(new Sphere()); HandleShape(new Shape());
  • 37.
    Classes and StructsAbstract Methods An abstract method is virtual and has no implementation Must belong to an abstract class Intended to be implemented in a derived class
  • 38.
    Classes and StructsAbstract Methods abstract class Shape { public abstract void Draw(); } class Box : Shape { public override void Draw() { ... } } class Sphere : Shape { public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... } HandleShape(new Box()); HandleShape(new Sphere()); HandleShape(new Shape()); // Error!
  • 39.
    Classes and StructsMethod Versioning Must explicitly use override or new keywords to specify versioning intent Avoids accidental overriding Methods are non-virtual by default C++ and Java product fragile base classes – cannot specify versioning intent
  • 40.
    Classes and StructsMethod Versioning class Derived: Base { // version 1 public virtual void Foo() { Console.WriteLine(&quot;Derived.Foo&quot;); } } class Base { // version 1 } class Base { // version 2 public virtual void Foo() { Console.WriteLine(&quot;Base.Foo&quot;); } } class Derived: Base { // version 2a new public virtual void Foo() { Console.WriteLine(&quot;Derived.Foo&quot;); } } class Derived: Base { // version 2b public override void Foo() { base.Foo(); Console.WriteLine(&quot;Derived.Foo&quot;); } }
  • 41.
    Classes and StructsConstructors Instance constructors are special methods that are called when a class or struct is instantiated Performs custom initialization Can be overloaded If a class doesn’t define any constructors, an implicit parameterless constructor is created Cannot create a parameterless constructor for a struct All fields initialized to zero/null
  • 42.
    Classes and StructsConstructor Initializers One constructor can call another with a constructor initializer Can call this(...) or base(...) Default constructor initializer is base() class B { private int h; public B() { } public B(int h) { this.h = h; } } class D : B { private int i; public D() : this(24) { } public D(int i) { this.i = i; } public D(int h, int i) : base(h) { this.i = i; } }
  • 43.
    Classes and StructsStatic Constructors A static constructor lets you create initialization code that is called once for the class Guaranteed to be executed before the first instance of a class or struct is created and before any static member of the class or struct is accessed No other guarantees on execution order Only one static constructor per type Must be parameterless
  • 44.
    Classes and StructsDestructors A destructor is a method that is called before an instance is garbage collected Used to clean up any resources held by the instance, do bookkeeping, etc. Only classes, not structs can have destructors class Foo { ~Foo() { Console.WriteLine(ā€œDestroyed {0}ā€, this); } }
  • 45.
    Classes and StructsDestructors Unlike C++, C# destructors are non-deterministic They are not guaranteed to be called at a specific time They are guaranteed to be called before shutdown Use the using statement and the IDisposable interface to achieve deterministic finalization
  • 46.
    Classes and StructsOperator Overloading User-defined operators Must be a static method class Car { string vid; public static bool operator ==(Car x, Car y) { return x.vid == y.vid; } }
  • 47.
    Classes and StructsOperator Overloading Overloadable unary operators Overloadable binary operators -- ++ false true ~ ! - + >= != ~ <= > < >> << == ^ | & % ! / * - +
  • 48.
    Classes and StructsOperator Overloading No overloading for member access, method invocation, assignment operators, nor these operators: sizeof , new , is , as , typeof , checked , unchecked , && , || , and ?: The && and || operators are automatically evaluated from & and | Overloading a binary operator (e.g. * ) implicitly overloads the corresponding assignment operator (e.g. *= )
  • 49.
    Classes and StructsOperator Overloading struct Vector { int x, y; public Vector(x, y) { this.x = x; this.y = y; } public static Vector operator +(Vector a, Vector b) { return Vector(a.x + b.x, a.y + b.y); } ... }
  • 50.
    Classes and StructsConversion Operators User-defined explicit and implicit conversions class Note { int value; // Convert to hertz – no loss of precision public static implicit operator double(Note x) { return ...; } // Convert to nearest note public static explicit operator Note(double x) { return ...; } } Note n = (Note)442.578; double d = n;
  • 51.
    Classes and StructsImplementing Interfaces Classes and structs can implement multiple interfaces A class or struct that inherits from an interface must implement all function members defined in that interface
  • 52.
    Classes and StructsImplementing Interfaces public interface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() { ... } } public class Car : IDelete { public void Delete() { ... } } TextBox tb = new TextBox(); IDelete iDel = tb; iDel.Delete(); Car c = new Car(); iDel = c; iDel.Delete();
  • 53.
    Classes and StructsImplementing Interfaces Explicit interface implementation Handles name collisions public interface IDelete { void Delete(); } public interface IFoo { void Delete(); } public class TextBox : IDelete, IFoo { public void IDelete.Delete() { ... } public void IFoo.Delete() { ... } }
  • 54.
    Classes and StructsNested Types Declared within the scope of another type Nesting a type provides three benefits: Nested type can access all the members of its enclosing type, regardless of access modifer Nested type can be hidden from other types Accessing a nested type from outside the enclosing type requires specifying the type name Nested types can be declared new to hide inherited types Unlike Java inner classes, nested types imply no relationship between instances
  • 55.
    Classes and Structsis Operator The is operator is used to dynamically test if the run-time type of an object is compatible with a given type static void DoSomething(object o) { if (o is Car) ((Car)o).Drive(); } Don’t abuse the is operator: it is preferable to design an appropriate type hierarchy with polymorphic methods
  • 56.
    Classes and Structsas Operator The as operator tries to convert a variable to a specified type; if no such conversion is possible the result is null static void DoSomething(object o) { Car c = o as Car; if (c != null) c.Drive(); } More efficient than using is operator: test and convert in one operation Same design warning as with the is operator
  • 57.
    Classes and Structstypeof Operator The typeof operator returns the System.Type object for a specified type Can then use reflection to dynamically obtain information about the type Console.WriteLine(typeof(int).FullName); Console.WriteLine(typeof(System.Int).Name); Console.WriteLine(typeof(float).Module); Console.WriteLine(typeof(double).IsPublic); Console.WriteLine(typeof(Car).MemberType);
  • 58.
    Delegates Overview Adelegate is a reference type that defines a method signature A delegate instance holds one or more methods Essentially an ā€œobject-oriented function pointerā€ Methods can be static or non-static Methods can return a value Provides polymorphism for individual functions Foundation for event handling
  • 59.
    Delegates Overview delegatedouble Del(double x); // Declare static void DemoDelegates() { Del delInst = new Del(Math.Sin); // Instantiate double x = delInst(1.0); // Invoke }
  • 60.
    Delegates Multicast DelegatesA delegate can hold and invoke multiple methods Multicast delegates must contain only methods that return void , else there is a run-time exception Each delegate has an invocation list Methods are invoked sequentially, in the order added The += and -= operators are used to add and remove delegates, respectively += and -= operators are thread-safe
  • 61.
    Delegates Multicast Delegatesdelegate void SomeEvent(int x, int y); static void Foo1(int x, int y) { Console.WriteLine(&quot;Foo1&quot;); } static void Foo2(int x, int y) { Console.WriteLine(&quot;Foo2&quot;); } public static void Main() { SomeEvent func = new SomeEvent(Foo1); func += new SomeEvent(Foo2); func(1,2); // Foo1 and Foo2 are called func -= new SomeEvent(Foo1); func(2,3); // Only Foo2 is called }
  • 62.
    Delegates and InterfacesCould always use interfaces instead of delegates Interfaces are more powerful Multiple methods Inheritance Delegates are more elegant for event handlers Less code Can easily implement multiple event handlers on one class/struct
  • 63.
    Events Overview Eventhandling is a style of programming where one object notifies another that something of interest has occurred A publish-subscribe programming model Events allow you to tie your own code into the functioning of an independently created component Events are a type of ā€œcallbackā€ mechanism
  • 64.
    Events Overview Eventsare well suited for user-interfaces The user does something (clicks a button, moves a mouse, changes a value, etc.) and the program reacts in response Many other uses, e.g. Time-based events Asynchronous operation completed Email message has arrived A web session has begun
  • 65.
    Events Overview C#has native support for events Based upon delegates An event is essentially a field holding a delegate However, public users of the class can only register delegates They can only call += and -= They can’t invoke the event’s delegate Multicast delegates allow multiple objects to register with the same event
  • 66.
    Events Example: Component-SideDefine the event signature as a delegate Define the event and firing logic public delegate void EventHandler(object sender, EventArgs e); public class Button { public event EventHandler Click; protected void OnClick(EventArgs e) { // This is called when button is clicked if (Click != null) Click(this, e); } }
  • 67.
    Events Example: User-SideDefine and register an event handler public class MyForm: Form { Button okButton; static void OkClicked(object sender, EventArgs e) { ShowMessage(&quot;You pressed the OK button&quot;); } public MyForm() { okButton = new Button(...); okButton.Caption = &quot;OK&quot;; okButton.Click += new EventHandler(OkClicked); } }
  • 68.
    Attributes Overview It’soften necessary to associate information (metadata) with types and members, e.g. Documentation URL for a class Transaction context for a method XML persistence mapping COM ProgID for a class Attributes allow you to decorate a code element (assembly, module, type, member, return value and parameter) with additional information
  • 69.
    Attributes Overview [HelpUrl(ā€œhttp://SomeUrl/APIDocs/SomeClassā€)]class SomeClass { [Obsolete(ā€œUse SomeNewMethod insteadā€)] public void SomeOldMethod() { ... } public string Test([SomeAttr()] string param1) { ... } }
  • 70.
    Attributes Overview Attributesare superior to the alternatives Modifying the source language Using external files, e.g., .IDL, .DEF Attributes are extensible Attributes allow to you add information not supported by C# itself Not limited to predefined information Built into the .NET Framework, so they work across all .NET languages Stored in assembly metadata
  • 71.
    Attributes Overview Somepredefined .NET Framework attributes COM Prog ID ProgId Transactional characteristics of a class Transaction Compiler will complain if target is used Obsolete Allows a class or struct to be serialized Serializable Should a property or event be displayed in the property window Browsable Description Attribute Name
  • 72.
    Attributes Overview Attributescan be Attached to types and members Examined at run-time using reflection Completely extensible Simply a class that inherits from System.Attribute Type-safe Arguments checked at compile-time Extensive use in .NET Framework XML, Web Services, security, serialization, component model, COM and P/Invoke interop, code configuration…
  • 73.
    Attributes Querying Attributes[HelpUrl(&quot;http://SomeUrl/MyClass&quot;)] class Class1 {} [HelpUrl(&quot;http://SomeUrl/MyClass&quot;), HelpUrl(&quot;http://SomeUrl/MyClassā€, Tag=ā€œctorā€)] class Class2 {} Type type = typeof(MyClass); foreach (object attr in type.GetCustomAttributes() ) { if ( attr is HelpUrlAttribute ) { HelpUrlAttribute ha = (HelpUrlAttribute) attr; myBrowser.Navigate( ha.Url ); } }
  • 74.
    C# provides preprocessordirectives that serve a number of functions Unlike C++, there is not a separate preprocessor The ā€œpreprocessorā€ name is preserved only for consistency with C++ C++ preprocessor features removed include: #include : Not really needed with one-stop programming; removal results in faster compilation Macro version of #define : removed for clarity Preprocessor Directives Overview
  • 75.
    Preprocessor Directives OverviewDelimit outline regions #region , #end Specify line number #line Issue errors and warnings #error , #warning Conditionally skip sections of code #if, #elif , #else , #endif Define and undefine conditional symbols #define , #undef Description Directive
  • 76.
    Preprocessor Directives ConditionalCompilation #define Debug public class Debug { [Conditional(&quot;Debug&quot;)] public static void Assert(bool cond, String s) { if (!cond) { throw new AssertionException(s); } } void DoSomething() { ... // If Debug is not defined, the next line is // not even called Assert((x == y), ā€œX should equal Yā€); ... } }
  • 77.
    Preprocessor Directives AssertionsBy the way, assertions are an incredible way to improve the quality of your code An assertion is essentially a unit test built right into your code You should have assertions to test preconditions, postconditions and invariants Assertions are only enabled in debug builds Your code is QA’d every time it runs Must read: ā€œ Writing Solid Code ā€, by Steve Maguire, Microsoft Press, ISBN 1-55615-551-4
  • 78.
    XML Comments OverviewProgrammers don’t like to document code, so we need a way to make it easy for them to produce quality, up-to-date documentation C# lets you embed XML comments that document types, members, parameters, etc. Denoted with triple slash: /// XML document is generated when code is compiled with /doc argument Comes with predefined XML schema, but you can add your own tags too Some are verified, e.g. parameters, exceptions, types
  • 79.
    XML Comments OverviewFormatting hints <list>, <item>, ... Use of a parameter <paramref> Cross references <see>, <seealso> Sample code <example>, <c>, <code> Property <value> Exceptions thrown from method <exception> Permission requirements <permission> Method return value <returns> Method parameter <param> Type or member <summary>, <remarks> Description XML Tag
  • 80.
    XML Comments Overviewclass XmlElement { /// <summary> /// Returns the attribute with the given name and /// namespace</summary> /// <param name=&quot;name&quot;> /// The name of the attribute</param> /// <param name=&quot;ns&quot;> /// The namespace of the attribute, or null if /// the attribute has no namespace</param> /// <return> /// The attribute value, or null if the attribute /// does not exist</return> /// <seealso cref=&quot;GetAttr(string)&quot;/> /// public string GetAttr(string name, string ns) { ... } }
  • 81.
    Unsafe Code OverviewDevelopers sometime need total control Performance extremes Dealing with existing binary structures Existing code Advanced COM support, DLL import C# allows you to mark code as unsafe, allowing Pointer types, pointer arithmetic -> , * operators Unsafe casts No garbage collection
  • 82.
    Unsafe Code OverviewLets you embed native C/C++ code Basically ā€œinline Cā€ Must ensure the GC doesn’t move your data Use fixed statement to pin data Use stackalloc operator so memory is allocated on stack, and need not be pinned unsafe void Foo() { char* buf = stackalloc char[256]; for (char* p = buf; p < buf + 256; p++) *p = 0; ... }
  • 83.
    Unsafe Code Overviewclass FileStream: Stream { int handle; public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, null); } return n; } [dllimport(&quot;kernel32&quot;, SetLastError=true)] static extern unsafe bool ReadFile(int hFile, void* lpBuffer, int nBytesToRead, int* nBytesRead, Overlapped* lpOverlapped); }
  • 84.
    Unsafe Code C#and Pointers Power comes at a price! Unsafe means unverifiable code Stricter security requirements Before the code can run Downloading code
  • 85.
    More Resources http://msdn.microsoft.comhttp://windows.oreilly.com/news/hejlsberg_0800.html http://www.csharphelp.com/ http://www.csharp-station.com/ http://www.csharpindex.com/ http://msdn.microsoft.com/msdnmag/issues/0900/csharp/csharp.asp http://www.hitmill.com/programming/dotNET/csharp.html http://www.c-sharpcorner.com/ http://msdn.microsoft.com/library/default.asp?URL=/library/dotnet/csspec/vclrfcsharpspec_Start.htm

Editor's Notes

  • #3Ā This module is designed to update C++ and Java programmers with the fundamentals of C#.
  • #5Ā The term object sometimes refers to an instance and sometimes to a class. It’s meaning can usually be determined by context.
  • #6Ā An interface is a well-defined set of methods (functions). Interfaces do not contain data members. Type is different from class. The type specifies the interfaces, the class specifies the implementation.
  • #7Ā Polymorphism is achieved through: Inheritance: a base type can have multiple derived types Interfaces: multiple types can implement a given interface Late binding: you can use any object, as long as it implements the methods you want to call. In C# you can use reflection to dynamically determine if an object implements a method, and then call it. Interfaces can also be used in a late-bound manner. In order to handle multiple types without polymorphism you have to write conditional code (using if or switch statements) that tests the type of an instance and then runs the appropriate code. Such code is brittle and not easily extended. Many Object-Oriented design concepts are motivated by minimizing dependencies. You want to be able to develop independent modules, so that making a change to one doesn’t force you to have to go back and change others.
  • #8Ā In scenarios where completely different objects need to support some kind of shared functionality like, let’s say, persist to XML, classes can implement interfaces that make them compatible with even if they don’t share the same base class. This provides most of the benefits of multiple class inheritance without the nasty side-effects that this usually brings. Interface members are implicitly public and abstract.
  • #11Ā If there’s no ambiguity then you do not have to specify the name of the interface.
  • #12Ā Classes and structs provide a way to create user-defined types.
  • #14Ā The C++ and C# structs are alike in name only.
  • #19Ā protected internal = protected OR internal. No way to define protected AND internal.
  • #26Ā Constants are compiled into client programs, while readonly fields are not.
  • #31Ā Having to declare ref in the caller helps make the code more readable and maintainable.
  • #34Ā In the example, the Add function can be called with a variable number of arguments. Here, there are 3 arguments. Each is put into the intArr array. The function then iterates over the array, producing the sum of the 3 numbers.
  • #35Ā When you call a non-virtual method M (DoSomething) on a class C (Foo), you are guaranteed that C.M (Foo.DoSomething) will be called.
  • #41Ā Let’s say that we have a base class that doesn’t do anything interesting. BUILD: Then a user of this class inherited this class and create a method named Foo() BUILD: Then the creator of the original base class realizes how Foo-ness is important and decided to implement that in the base class. Then we have a problem. How should this behave? Should the clients keep calling Derived.Foo or Base.Foo. In C# it is guaranteed that Derived.Foo will be the one being called because it will most likely preserve the behaviour expected by the client. However the compiler will issue an warning saying that method Base.Foo was hidden. BUILD: To suppress the warning, the programmer can use the keyword new, and explicitly hide the method Base.Foo. BUILD: Or if he can just override as any virtual method.
  • #42Ā If a class defines any constructors, then the implicit parameterless constructor is not created.
  • #43Ā B.B() is needed because once B.B(int h) was defined, the default parameterless constructor is no longer created. Can’t call both : base() and : this() because it would then be ambiguous as to how the base class is constructed.
  • #55Ā Java inner classes implicitly have access to the instance of their containing class that created them. C# nested types have no such implicit relationship. However, such a relationship can easily be implemented by passing a reference to the containing class to the nested class, which then saves that in a field.
  • #59Ā Delegates are essentially function pointers. This is the mechanism used to implement event in the frameworks. In the example we first declare a delegate for a function that returns double and receives a double as a parameter. We then instantiate the delegate, telling it to use Math.Sin as the function. Finally we invoke the delegate we created. The power is in the fact that once instantiated, a delegate can be passed around and then invoked at some later time.
  • #73Ā Since attribute values are stored with the generated code and can be examined by the code in run-time through a mechanism called reflection, you can infinitely add new attributes that will extend the functionality of your C# code. So just as an example, as a CORBA vendor, instead of having to create CORBA wrappers and utilities to write marshalling code, you can define a set of attributes that will instruct the CORBA broker how to marshal the data, also this could be applied to do add information on how to do object to relational database mapping or XML persistence.
  • #77Ā Compiler directives are supported just like in C++ with a notable exception of Macros. The reason is that macros can change the behavior of the code just by changing a define. It also makes debugger really harder and also makes a lot more difficult to make proper optimizations. However, there is a attribute called Conditional that can be used to suppress methods based on a define. This is how a type-safe Assert is implemented in C# without macros.
  • #83Ā If you need to do some real low level memory manipulation, you can declare a method unsafe and then you have access to full C pointer syntax. You can think of unsafe methods as inline C.
  • #84Ā Just declare the unsafe keyword and you can do full memory manipulation