Programming Visual CSharp 2005

Programming visual C# 2005: The Language by Donis Marshall

Book highlights for me:
Below is a brief list of some of the language highlights presented in "Programming Visual C# 2005" that I have finally been able to get my hands on.

Nullable types

  • //Define a nullable type
    int? a = null;
  • //Boxing a nullable type
    object b = (object)a;
  • //Detecting if a boxed value is a nullable type
    if(b.GetType().IsGenericType && b.GetType.GetGenericTypeDefinition() == typeof(Nullable<>))
  • //Avoiding null
    a.GetValueOrDefault(0);
  • //Setting a default value
    a = value ?? 0;

Weak references

  • //Using weak reference is a way of pointing to an object's value, but it's special because when our friend the garbage collector comes along, if the object has no hard references it is marked for collection.
    WeakReference a = new WeakReference(new object());

Anonymous methods

  • //Yes, I remember these from my uni Java days. Anonymous methods have helped me successfully program many unmaintainable Java classes, all I have to say about this is: they are great but, use with caution.
    SomeDelegate del = delegate()
    {
    MessageBox.Show("Hi!");
    }
  • //Passing Parameters to anonymous methods.
    SomeDelegate del = delegate(string str)
    {
    MessageBox.Show(str);
    }
  • Generic anonymous methods...you get the picture, they do it all...

Delegate Improvements

  • //Delegate inference, see, point the equals sign at the function you want it to call...and make it do stuff good.
    delegate void MyDelegate;
    void SomeFunction()
    {
    MyDelegate del = AnotherFunction;
    del();
    }
    void AnotherFunction(){ ... }

Garbage Collection

  • // Memory pressure, this would be used in a wrapper class to indicate that a large amount of unmanaged memory is going to be consumed.
    GC.AddMemoryPressure(60000);

Generics

  • //Generically typed built-ins such as collections...whoa! so good!
    ArrayList<string> strAr = new ArrayList<string>();
  • Generic classes, functions, etc...

Other

  • //This is not new, and I haven't really come across too many situations where I've needed bitwise enumeration, however, I'll note down the flag just in case.
    [flag]
    public enum MyEnum{ A = 1, B = 2, C = 3 }
    MyEnum var = MyEnum.A | MyEnum.B;
  • It's suprising how many times this is handy...
    string.IsNullOrEmty();
  • Partial Classes...mmmm...C++ had this kind of good stuff too...

Comments on the book:
In general I'd say that this book is pretty well presented, the code samples seem straight forward and practical. I'd probably recommend it if you are familiar with .Net or have a good idea about what .Net 2.0 offers. I don't know if an absolute programming newbie would understand it from the word go, but I could be wrong. So as an introduction and general reference to 2.0 it should be a good book to have around.

General .NET Product Reviews
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

No comments yet. Be the first!

No new comments are allowed on this post.