SlideShare a Scribd company logo
1 of 85
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 ,[object Object],[object Object],[object Object],[object Object]
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Review  Key Object-Oriented Concepts
Review  Key Object-Oriented Concepts ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Review  Key Object-Oriented Concepts ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Interfaces ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],interface IControl {   void Paint(); } interface IListBox: IControl {   void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { }
Interfaces Explicit Interface Members ,[object Object],interface IControl {   void Delete(); } interface IListBox: IControl {   void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); }
Classes and Structs Similarities ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],Members are always  public Always allocated on the stack or  as a member ,[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Access Modifiers ,[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Access Modifiers to T or types derived from T ,[object Object],within T only (the default) ,[object Object],[object Object],protected internal to types within A ,[object Object],[object Object],[object Object],Then a member defined in type T and assembly A is accessible If the access  modifier is
Classes and Structs Abstract Classes ,[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Sealed Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs this ,[object Object],[object Object],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 ,[object Object],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 ,[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Readonly Fields ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],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 ,[object Object],[object Object],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; ,[object Object]
Classes and Structs Indexers ,[object Object],[object Object],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]); ,[object Object]
Classes and Structs Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Method Argument Passing ,[object Object],[object Object],[object Object],[object Object]
Classes and Structs Method Argument Passing ,[object Object],[object Object],[object Object],[object Object],void RefFunction(ref int p) { p++; } int x = 10; RefFunction(ref x); // x is now 11
Classes and Structs Method Argument Passing ,[object Object],[object Object],[object Object],[object Object],void OutFunction(out int p) { p = 22; } int x; OutFunction(out x); // x is now 22
Classes and Structs Overloaded Methods ,[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],class Foo { public void DoSomething(int i) { ... } } Foo f = new Foo(); f.DoSomething();
Classes and Structs Virtual Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Constructor Initializers ,[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs Destructors ,[object Object],[object Object],[object Object],class Foo { ~Foo() {  Console.WriteLine(“Destroyed {0}”, this);  } }
Classes and Structs Destructors ,[object Object],[object Object],[object Object],[object Object]
Classes and Structs Operator Overloading ,[object Object],[object Object],class Car { string vid; public static bool operator ==(Car x, Car y) { return x.vid == y.vid; } }
Classes and Structs Operator Overloading ,[object Object],[object Object],-- ++ false true ~ ! - + >= != ~ <= > < >> << == ^ | & % ! / * - +
Classes and Structs Operator Overloading ,[object Object],[object Object],[object Object]
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 ,[object Object],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 ,[object Object],[object Object]
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 ,[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Classes and Structs is  Operator ,[object Object],static void DoSomething(object o) { if (o is Car)  ((Car)o).Drive(); } ,[object Object]
Classes and Structs as  Operator ,[object Object],static void DoSomething(object o) { Car c = o as Car; if (c != null) c.Drive(); } ,[object Object],[object Object]
Classes and Structs typeof  Operator ,[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Events Overview ,[object Object],[object Object],[object Object],[object Object]
Events Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Events Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Events Example: Component-Side ,[object Object],[object Object],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 ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Attributes Overview [HelpUrl(“http://SomeUrl/APIDocs/SomeClass”)] class SomeClass { [Obsolete(“Use SomeNewMethod instead”)] public void SomeOldMethod() {  ...  } public string Test([SomeAttr()] string param1) { ...  } }
Attributes Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Attributes Overview ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ); } }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
XML Comments Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Unsafe Code Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object]
More Resources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaEdureka!
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
Vb ch 3-object-oriented_fundamentals_in_vb.net
Vb ch 3-object-oriented_fundamentals_in_vb.netVb ch 3-object-oriented_fundamentals_in_vb.net
Vb ch 3-object-oriented_fundamentals_in_vb.netbantamlak dejene
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceOum Saokosal
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interfaceMazharul Sabbir
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Ricardo Quintero
 

What's hot (20)

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Vb ch 3-object-oriented_fundamentals_in_vb.net
Vb ch 3-object-oriented_fundamentals_in_vb.netVb ch 3-object-oriented_fundamentals_in_vb.net
Vb ch 3-object-oriented_fundamentals_in_vb.net
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Chap11
Chap11Chap11
Chap11
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
javainterface
javainterfacejavainterface
javainterface
 
C# interview
C# interviewC# interview
C# interview
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5
 

Viewers also liked

Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Binu Bhasuran
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examplesQuang Suma
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)Koen Metsu
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkBinu Bhasuran
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLMohammad Shaker
 
Advanced C#. Part 2
Advanced C#. Part 2Advanced C#. Part 2
Advanced C#. Part 2eleksdev
 
Advanced C#. Part 1
Advanced C#. Part 1Advanced C#. Part 1
Advanced C#. Part 1eleksdev
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel ProgrammingAlex Moore
 
What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5Robert MacLean
 
Patterns For Cloud Computing
Patterns For Cloud ComputingPatterns For Cloud Computing
Patterns For Cloud ComputingSimon Guest
 
Web Application Development Fundamentals
Web Application Development FundamentalsWeb Application Development Fundamentals
Web Application Development FundamentalsMohammed Makhlouf
 
Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web ArchitectureChamnap Chhorn
 

Viewers also liked (17)

Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility Framework
 
.Net 3.5
.Net 3.5.Net 3.5
.Net 3.5
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XML
 
Advanced C#. Part 2
Advanced C#. Part 2Advanced C#. Part 2
Advanced C#. Part 2
 
Advanced C#. Part 1
Advanced C#. Part 1Advanced C#. Part 1
Advanced C#. Part 1
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5What’s new in Visual Studio 2012 & .NET 4.5
What’s new in Visual Studio 2012 & .NET 4.5
 
Patterns For Cloud Computing
Patterns For Cloud ComputingPatterns For Cloud Computing
Patterns For Cloud Computing
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
SOA Unit I
SOA Unit ISOA Unit I
SOA Unit I
 
Web Application Development Fundamentals
Web Application Development FundamentalsWeb Application Development Fundamentals
Web Application Development Fundamentals
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web Architecture
 

Similar to Advanced c#

Similar to Advanced c# (20)

20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegan
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
C# program structure
C# program structureC# program structure
C# program structure
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
1204csharp
1204csharp1204csharp
1204csharp
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Templates
TemplatesTemplates
Templates
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Recently uploaded

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesSHIVANANDaRV
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 

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.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. 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();
  • 9.
  • 10.
  • 11.
  • 12. 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
  • 13.
  • 14. 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();
  • 15. 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
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36. 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());
  • 37.
  • 38. 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!
  • 39.
  • 40. Classes and Structs Method 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.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49. 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); } ... }
  • 50.
  • 51.
  • 52. 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();
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59. Delegates Overview delegate double Del(double x); // Declare static void DemoDelegates() { Del delInst = new Del(Math.Sin); // Instantiate double x = delInst(1.0); // Invoke }
  • 60.
  • 61. 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 }
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69. Attributes Overview [HelpUrl(“http://SomeUrl/APIDocs/SomeClass”)] class SomeClass { [Obsolete(“Use SomeNewMethod instead”)] public void SomeOldMethod() { ... } public string Test([SomeAttr()] string param1) { ... } }
  • 70.
  • 71.
  • 72.
  • 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.
  • 75. 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
  • 76. 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”); ... } }
  • 77.
  • 78.
  • 79. 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
  • 80. 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) { ... } }
  • 81.
  • 82.
  • 83. 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); }
  • 84.
  • 85.

Editor's Notes

  1. This module is designed to update C++ and Java programmers with the fundamentals of C#.
  2. The term object sometimes refers to an instance and sometimes to a class. It’s meaning can usually be determined by context.
  3. 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.
  4. 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.
  5. 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.
  6. If there’s no ambiguity then you do not have to specify the name of the interface.
  7. Classes and structs provide a way to create user-defined types.
  8. The C++ and C# structs are alike in name only.
  9. protected internal = protected OR internal. No way to define protected AND internal.
  10. Constants are compiled into client programs, while readonly fields are not.
  11. Having to declare ref in the caller helps make the code more readable and maintainable.
  12. 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.
  13. 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.
  14. 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.
  15. If a class defines any constructors, then the implicit parameterless constructor is not created.
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. Just declare the unsafe keyword and you can do full memory manipulation