Advanced C: John Syre
Advanced C: John Syre
John Syre
About CDS
Collaborative Data Services Collaborative Data Services Providing "data services" through FHCRC Providing "data services" through FHCRC (and beyond) Our services include Our services include
Telephone interviewing of subjects Telephone interviewing of subjects Data entry & scanning Data entry & scanning Programming Web and database hosting Web and database hosting
Agenda
Review Object-Oriented Concepts Classes (..and Structs) Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Threads
Review
Key Object-Oriented Concepts
Objects, instances and classes Identity
Every instance has a unique identity, regardless of its data (how this keyword works) Data and function are packaged together Information hiding An object is an abstraction
Encapsulation
Review
Key Object-Oriented Concepts
Interfaces
A well-defined contract A set of function members An object has a type, which specifies its interfaces and their implementations A variable also can have a type Types are arranged in a hierarchy
Types
Inheritance
Base/derived, superclass/subclass
Review
Key Object-Oriented Concepts
Polymorphism
The ability to use an object without knowing its precise type Three main kinds of polymorphism
Dependencies
For reuse and to facilitate development, systems should be loosely coupled Dependencies should be minimized (the more dependent the OO system is, the more brittle it will be)
Agenda
Review Object-Oriented Concepts Classes (..and Structs) Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Threads
Data
Fields, constants, events, arrays Methods, properties, indexers, operators, constructors Classes, structs, enums, interfaces, delegates
Functions
Type definitions
Struct
C# Struct
User-defined value type
Each instance gets its own fields Methods apply to a specific instance Static methods cant access instance data No this variable in static methods They are essentially object-oriented global data and global functions
protected internal
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
a class instance (a reference), a struct instance (actual data), or an array of class or struct instances (an array is actually a reference)
public class MyClass { public static readonly double d1 = Math.Sin(Math.PI); public readonly string s1; public MyClass(string s) { s1 = s; } }
Can be
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
Derived classes provide their own specialized implementation Use abstract method if no default implementation
class Car { string vid; public static bool operator ==(Car x, Car y) { return x.vid == y.vid; } }
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); } ... }
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
Dont abuse the is operator: it is preferable to design an appropriate type hierarchy with polymorphic methods
More efficient than using is operator: test and convert in one operation Same design warning as with the is operator
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs) Threads
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 When a class or struct implements an interface it must provide the implementation 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 invoke the intended method
interface IControl { void Delete(); } interface IListBox: IControl { void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); }
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs) Threads
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
Delegates
Overview
Example:
delegate double Del(double x); static void DemoDelegates() { Del delInst = new Del(Math.Sin); double x = delInst(1.0); } // Declare
// Instantiate // 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 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("Foo1"); } static void Foo2(int x, int y) { Console.WriteLine("Foo2"); } 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 Less code Can easily implement multiple event handlers on one class/struct
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs) Threads
Events
Overview
Event handling is a style of programming where one object notifies another that something of interest has occurred
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 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 cant invoke the events delegate
Multicast delegates allow multiple objects to register with the same event
Events
Example: Component-Side
Define the event signature as a delegate
public delegate void EventHandler(object sender, EventArgs e);
Events
Example: User-Side
Define and register an event handler
public class MyForm: Form { Button okButton;
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs) Threads
Attributes
Overview
Its 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 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
Attributes
Overview
Some predefined .NET Framework attributes
Attribute Name
Browsable Serializable Obsolete ProgId Transaction
Description
Should a property or event be displayed in the property window Allows a class or struct to be serialized Compiler will complain if target is used COM Prog ID Transactional characteristics of a class
Attributes
Overview
Attributes can be
Completely extensible
Type-safe
Attributes
Querying Attributes
[HelpUrl("http://SomeUrl/MyClass")] class Class1 {} [HelpUrl("http://SomeUrl/MyClass"), HelpUrl("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 ); } }
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs) Threads
Preprocessor Directives
Overview
C# provides preprocessor directives that serve a number of functions Unlike C++, there is not a separate preprocessor
Preprocessor Directives
Overview
Directive
#define, #undef #if, #elif, #else, #endif #error, #warning
Description
Define and undefine conditional symbols Conditionally skip sections of code Issue errors and warnings
#region, #end
#line
Preprocessor Directives
Conditional Compilation
#define Debug public class Debug { [Conditional("Debug")] 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 QAd every time it runs Must read: Writing Solid Code, by Steve Maguire, Microsoft Press, ISBN 1-55615-551-4
Agenda
Review Object-Oriented Concepts Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Classes (..and Structs)
XML Comments
Overview
Programmers dont 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.
Comes with predefined XML schema, but you can add your own tags too
XML Comments
Overview
XML Tag
<summary>, <remarks>
Description
Type or member
<param> <returns>
<exception> <example>, <c>, <code> <see>, <seealso> <value>
<paramref>
<list>, <item>, ... <permission>
Use of a parameter
Formatting hints Permission requirements
XML Comments
Overview
class XmlElement { /// <summary> /// Returns the attribute with the given name and /// namespace</summary> /// <param name="name"> /// The name of the attribute</param> /// <param name="ns"> /// 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="GetAttr(string)"/> /// public string GetAttr(string name, string ns) { ... } }
Agenda
Review Object-Oriented Concepts Classes (..and Structs) Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Threads
Unsafe Code
Overview
Developers sometime need total control
Performance extremes Dealing with existing binary structures Existing code Advanced COM support, DLL import 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 doesnt 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("kernel32", 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!
Agenda
Review Object-Oriented Concepts Classes (..and Structs) Interfaces Delegates Events Attributes Preprocessor Directives XML Comments Unsafe Code Threads
Threads Threads
Guidelines for use
Use when you need to need to do multiple tasks Can greatly improve the responsiveness in User Interfaces Insure the implementation is thread safe
One thread should not access a value while another is altering it Use synchronization to control thread access
Avoid the, If one thread is good then many more would be better
Threads Threads
Steps to implement
Decide on the method to be run by the thread (can be static or instance) Method must have the signature void SomeMethod(); Instantiate a thread delegate (System.Threading.ThreadStart) Create the thread, represented by System.Threading.Thread Finally, you start the thread using the Thread.Start() method.
Threads
Example
class ProcessFiles { public void LongRunning() { // body of the method } ProcessFiles worker = new ProcessFiles(); ThreadStart workerThreadStart = new ThreadStart(worker.LongRunning); Thread workerThread = new Thread(workerThreadStart); workerThread.Start(); }
Threads Threads
Microsoft Best Practices
Don't use Thread.Abort to terminate other threads. Don't use Thread.Suspend and Thread.Resume to synchronize the activities of multiple threads use Mutex, ManualResetEvent, AutoResetEvent, and Monitor. Don't control the execution of worker threads from your main program (using events, for example). Design worker threads that are responsible for waiting until work is available, executing it, and notifying other parts of your program when finished. Do ensure that a thread that has entered a monitor always leaves that monitor,even if an exception occurs while the thread is in the monitor (use lock) Do use multiple threads for tasks that require different resources, and avoid assigning multiple threads to a single resource.
Unsafe Code
Thread safe singleton pattern
public sealed class Singleton { private static volatile Singleton instance; private static object syncRoot = new Object(); private Singleton() {} public static Singleton Instance { get { if (instance == null) { lock (syncRoot) { if (instance == null) instance = new Singleton(); } } return instance; } }
}
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/cs harp.asp http://www.hitmill.com/programming/dotNET/csharp.html http://www.c-sharpcorner.com/ http://msdn.microsoft.com/library/default.asp?URL=/libr ary/dotnet/csspec/vclrfcsharpspec_Start.htm