Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Saturday, 2 February 2013

Maybe class for null handling for reference types in C#

"Tony Hoare, the inventor of the null reference, called it his ‘billion dollar mistake.’ Null is a disaster because it means that every reference type has a magic value that will destroy your program. For value types, we now have ‘normal’ value types and nullable value types to express the difference between, say, ‘this function returns an integer’ and ‘this function might return an integer, or it might not.’ But we don’t have that for reference types."
For example, in C# we have int for a normal value and int? for a nullable value. But for reference type like a string, you just don't know until you check it or your program crashed on it.

Ivan Towlson has mentioned a good and compact way to handle nullable type with a Maybe class. Since I don't have his source code, I created my own. If you are interested to use it, just simply download the Maybe.cs class under the MaybeApplication folder from github and use it in your own program. Or you can download the whole project. The project is created in VS2010.

Why should you care? What does the class do?

Ok, instead of doing the null check the old fashion way
        static string DemoTheOldWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            if (contact == null)
                return null;

            if (contact.PersonalPet == null)
                return null;

            if (contact.PersonalPet.PetName == null)
                return null;

            var petName = contact.PersonalPet.PetName.Name;
            if (petName == null)
                return null;

            return petName;
        }

Now with some Linq awesomeness, you can do something like this
        static string DemoTheNewWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            // Maybe class allow you to do something similar to int? for the reference types.
            Maybe maybeContact = new Maybe(contact);

            var maybePetName = from c in maybeContact
                          from cp in c.PersonalPet
                          from pn in cp.PetName
                          select pn.Name;

            if (maybePetName.HasValue)
                return maybePetName.Value;

            return null;
        }


Reference:
http://www.mindscapehq.com/blog/index.php/2012/03/27/5-12-f-features-every-c-programmer-should-lust-after/

Sunday, 25 March 2012

Indexer - Class member

Indexers are syntactic convenience that enable you to create a class, struct or interface that client application can accessed just as an array.

Static readonly vs const

The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, whereas the value of a const field is set to a compile time constant.

In the static readonly case, the containing class is allowed to modify it only

in the variable declaration (through a variable initializer)
in the static constructor (instance constructors, if it's not static)

http://blogs.msdn.com/b/csharpfaq/archive/2004/12/03/274791.aspx

Const values are burned directly into the call-site; this is double edged:

it is useless if the value is fetched at runtime, perhaps from config
if you change the value of a const, you need to rebuild all the clients
but it can be faster, as it avoids a method call...
...which might sometimes have been inlined by the JIT anyway
If the value will never change, then const is fine - Zero etc make reasonable consts ;-p Other than that, static properties are more common.

http://stackoverflow.com/questions/755685/c-static-readonly-vs-const