7/31/2019 SIGCSE Intermediate Topics in Csharp 003
1/32
Intermediate topics in C#
Ivan Lumala
Academic Developer Evangelist
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
2/32
Intermediate C#
1st class component support
Operator overloading
Versioning
Attributes
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
3/32
Component Support
What defines a component? Properties, methods, events
Integrated help and documentation
Design-time and runtime information
C# has first class support
Not naming patterns, helper classes, etc.
Not external files
Easy to build and consume
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
4/32
Properties
Access to private fields normally requiresaccessors and mutators
Properties allow access to private fields as ifthey were public
instead of:
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
5/32
Indexers
Index an object as if it were an array
()
()
instead of:
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
6/32
Delegates Object oriented function pointers
A reference type encapsulating a method signature andreturn value
Can point to multiple functions
Thread-safe + and - operations
Foundation for events
Delegate Multicast Delegate
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
7/32
Events
Specialized multicast delegate
Represent a signal that somethinghas occurred in a program
Two main issues
Generating events
Consuming events
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
8/32
EventsSourcing Define the event signature
Define the event and firing logic
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
9/32
EventsHandling Define and register event handler
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
10/32
Intermediate C#
Code example of Event Handling ina Windows Form
IsPrime app
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
11/32
Intermediate C#
1st class component support
Operator overloading
Versioning
Attributes
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
12/32
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);
Rational r1 = new Rational(1,2);
Rational r2 = 2;
Rational r3 = r1 + r2;
double d = (double) r3;
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
13/32
Rational Number Struct
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
14/32
Implicit Conversions
No loss of data
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
15/32
Explicit Conversions
Possible loss of precision and canthrow exceptions
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
16/32
Operator Overloading
Static operators Must take its type as a parameter
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
17/32
Equality Operators
.NET Framework equality support
.Equals() should use operator==()
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
18/32
Intermediate C#
1st class component support Operator overloading
Versioning
Attributes
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
19/32
Versioning
C# added some smarts to fix the versionproblem. Problem was that user could not express
versioning intent
C# can't guarantee versioning, but Can enable ( explicit override)
Can encourage ( smart defaults)
Impact on language is subtle but pervasive
Explicit interface implementation Overloading and overriding
readonly fields
Smart defaults for accessibility and virtuality
Event arguments packaged as objects
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
20/32
Method Versioning in C#
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
21/32
Intermediate C#
1st class component support Operator overloading
Versioning
Attributes
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
22/32
Attributes
How do you associate information withtypes and members?
Documentation URL for a class
Transaction context for a method XML persistence mapping
Traditional solutions
Add keywords or pragmas to language Use external files, e.g., IDL, DEF
C# solution: Attributes
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
23/32
Attributes
Appear in square brackets Attached to code elements
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
24/32
Attribute Fundamentals Can be Intrinsic or Custom
Attributes are generic classes!
Easy to attach to types and members
Attributes can be queried at runtime
class HelpUrl : System.Attribute {public HelpUrl(string url) { }
}
[HelpUrl(http://SomeUrl/APIDocs/SomeClass)]class SomeClass { }
Type type = Type.GetType(SomeClass);
Attribute[] attributes =type.GetCustomAttributes();
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
25/32
Creating Attributes
Attributes are simply classes Derived from System.Attribute
Class functionality = attribute
functionality
public class HelpURLAttribute: System.Attribute{
public HelpURLAttribute(string url) { }
public string URL { get { } }public string Tag { get { } set { } }
}
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
26/32
Using Attributes
Just attach it to a class
Use named parameters
Use multiple attributes
[HelpURL(http://someurl/)]Class MyClass { }
[HelpURL(http://someurl/, Tag=ctor)]Class MyClass { }
[HelpURL(http://someurl/),HelpURL(http://someurl/, Tag=ctor)]
Class MyClass { }
http://someurl/http://someurl/http://someurl/http://someurl/http://someurl/http://someurl/7/31/2019 SIGCSE Intermediate Topics in Csharp 003
27/32
Querying Attributes
Use reflection to query attributes
Type type = typeof(MyClass);
foreach(object attr in type.GetCustomAttributes())
{if(attr is HelpURLAttribute){
HelpURLAttribute ha = (HelpURLAttribute) attr;myBrowser.Navigate(ha.URL);
}}
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
28/32
Resources
Newsgroup:microsoft.public.dotnet.languages.CSharp onmsnews.microsoft.com
Web sites:
http://msdn.microsoft.com/
Books
Inside C# MS Press
C# Language Specification MS Press C# How to Program Prentice Hall
Training developmentor, Wintellect
http://www.microsoft.com/net/training.asp
http://msdn.microsoft.com/http://msdn.microsoft.com/7/31/2019 SIGCSE Intermediate Topics in Csharp 003
29/32
The .NET Framework in Curriculum
Multi-language runtime environmentUse the language you like
Access the same class libraries to do similartasks
Use a powerful IDE to access easy-to-uselearning tools
Visual Studio .NET Academic
Experience programming with .NET bybuilding your own Terrarium creature
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
30/32
Microsoft Resources for Faculty
MSDN Academic Alliance
Check out MSDN AANew program from Microsoft
Software for computer science courses
Annual membership fee of $799 perdepartmentMembership runs from July-June
Web site that supports program:
(www.msdnaa.net)Visual Studio .NET AcademicAll the features of Visual Studio .NET
Professional plus Course Management
Tools
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
31/32
Questions?
7/31/2019 SIGCSE Intermediate Topics in Csharp 003
32/32