Wednesday, June 3, 2009

Microsoft .NET Framework Application Development Foundation Training Kit - Chapter 1 - 'Rich' Notes

This is the first of many posts to come to aid in reviewing what i have learned from the Training Kit prior to taking the MCTS Exam 70-536.

Structs vs Classes

Custom Operators (==, + etc.)

Reference vs Value Types

Nullable Types (can only be value types)

Generics & Constraints on Generic classes

Events/Delegates
class Program
{
static void Main(string[] args)
{
DelegateTest test = new DelegateTest();
test.OnTimeChangedInstance += new DelegateTest.OnTimeChanged(test_OnTimeChanged);
test.RunTest();
}

private static void test_OnTimeChanged(DateTime d)
{
System.Diagnostics.Debug.WriteLine(d.ToLongTimeString());
}
}
class DelegateTest
{
public delegate void OnTimeChanged(DateTime newTime);
public OnTimeChanged OnTimeChangedInstance;

public void RunTest()
{
if (OnTimeChangedInstance != null)
OnTimeChangedInstance(DateTime.Now);
}
}

Type Forwarding
Allows you to move a type from assembly X into assembly Y for an application that is currently deployed and only has knowledge of assembly X without having to recompile the application. In assembly Y you create the type as it was in assembly X. Then in assembly X you remove the type that was moved to Y and replace it with an Assembly attribute that specifies the new location of the moved type. E.g. move type Car from assembly X to Y would result in assembly X no longer containing type Car, Assembly Y now containing Type Car and Assembly X adding the attribute [assembly:typeforwardedto(typeof(Y.Car))].http://www.xhydra.com/mcts-70-536/type-forwarding.html

Conversion Between Types
• System.Convert – converts between types that implement System.IConvertible.
• Cast operator – (int) obj.
• Type.Parse, Type.ToString
• Type.TryParse, Type.TryParseExact.
• Narrowing/Widening Conversions – Narrowing Conversions occur when the range or precision of the source type exceeds that of the destination type.

How to Implement Conversion in Custom Types
• Define conversion operators to simplify narrowing (explicit in c#) and widening (implicit in c#) conversions between numeric types.
• Override ToString to provide conversion to strings, override Parse to provide conversion from strings.
• Implement System.IConvertible to enable conversion though System.Convert. The interface has ~ 17 methods to implement – ToBoolean() etc.

Boxing/Unboxing
• Is the conversion between reference and value types.
• Should be avoided due to overhead
• All struct’s you create inherit from system.object which exposes the ToString method. Why would you want to override the ToString method in a struct? Avoids boxing the custom struct (value type) to the object type (reference type) in order to call ToString.

No comments: