Professional Documents
Culture Documents
SIGCSE Intermediate Topics in Csharp 003
SIGCSE Intermediate Topics in Csharp 003
SIGCSE Intermediate Topics in Csharp 003
Ivan Lumala
Academic Developer Evangelist ivanl@microsoft.com
Intermediate C#
Component Support
Properties, methods, events Integrated help and documentation Design-time and runtime information Not naming patterns, helper classes, etc. Not external files
Properties
Access to private fields normally requires accessors and mutators Properties allow access to private fields as if they were public
public class Person { private int age; public int Age { Property get { return age; } set { age = value; Console.WriteLine(age); } } }
C#
instead of:
Other
Indexers
public class numbers { private int[3] nums; public int this[int index] { get { return nums[index]; } set { nums[index] = value; } } }
instead of:
Numbers Num = new Numbers(); Num.Set_Nums(0,5); int Val = Num.Get_Nums(0);
Indexer
Other
Delegates
delegate double Func(double x); Func func = new Func(Math.Sin); double x = func(1.0);
Events
Specialized multicast delegate Represent a signal that something has occurred in a program Two main issues
Events
Sourcing
public class Button { public event SomeDelegate Click; protected void OnClick(EventArgs e) { if (Click != null) Click(this, e); } }
Events
Handling
Intermediate C#
Intermediate C#
Rational Number
, , 1
Rational r1 = new Rational(1,2); Rational r2 = new Rational(2,1); Rational r3 = r1.AddRational(r2); double d = Rational.ConvertToDouble(r3);
Implicit Conversions
No loss of data
public struct Rational { public static implicit operator Rational(int i) { return new Rational(i,1); } }
Rational r = 2;
Explicit Conversions
public struct Rational { public static explicit operator double(Rational r) { return (double) r.Numerator / r.Denominator; } }
Operator Overloading
public struct Rational { public static Rational operator+ ( Rational lhs,Rational rhs) { return new Rational( ); } } Rational r3 = r1 + r2;
r3 += 2;
Equality Operators
public static bool operator== (Rational lhs, Rational rhs) public static bool operator!= (Rational lhs, Rational rhs)
if ( r1.Equals(r2) ) { } if ( r1 == r2 ) { }
if ( !r1.Equals(r2)) { } if ( r1 != r2 ) { }
Intermediate C#
Versioning
Problem was that user could not express versioning intent Can enable ( explicit override) Can encourage ( smart defaults) Explicit interface implementation Overloading and overriding readonly fields Smart defaults for accessibility and virtuality Event arguments packaged as objects
Method Versioning in C#
class Base // // v2.0 v1.0 { } public virtual voidFoo() int Foo() { Database.Log("Base.Foo"); return 0; } } } } class Derived : Base // v1.0 v2.0 { public virtual voidvoid Foo() new virtual Foo() override void Foo() { Console.WriteLine("Derived.Foo"); super.Foo(); } Console.WriteLine("Derived.Foo"); } } }
Intermediate C#
Attributes
Documentation URL for a class Transaction context for a method XML persistence mapping
Add keywords or pragmas to language Use external files, e.g., IDL, DEF
Traditional solutions
C# solution: Attributes
Attributes
[HelpUrl(http://SomeUrl/Docs/SomeClass)] class SomeClass { [WebMethod] void GetCustomers() { } string Test([SomeAttr] string param1) {} }
Attribute Fundamentals
Creating Attributes
public class HelpURLAttribute: System.Attribute { public HelpURLAttribute(string url) { } public string URL { get { } } public string Tag { get { } set { } } }
Using Attributes
Querying Attributes
Type type = typeof(MyClass); foreach(object attr in type.GetCustomAttributes()) { if(attr is HelpURLAttribute) { HelpURLAttribute ha = (HelpURLAttribute) attr; myBrowser.Navigate(ha.URL); } }
Resources
Newsgroup:
microsoft.public.dotnet.languages.CSharp on
http://msdn.microsoft.com/ Inside C# MS Press C# Language Specification MS Press C# How to Program Prentice Hall
developmentor, Wintellect http://www.microsoft.com/net/training.asp
Books
Training
runtime environment
the language you like Access the same class libraries to do similar tasks
Use
Experience
Academic Alliance
Check
Annual
Web
Visual
All
the features of Visual Studio .NET Professional plus Course Management Tools
Questions?