Geeks With Blogs
Quixotry blog November 2005 Entries
Ever wanted to have the "as" operator for value types?
Now you can, with .NET 2.0, as suggested by Roy Osherove http://weblogs.asp.net/rosh... 1: /// Works like a strongly typed "as" operator2: /// If the object is not of the requested type, 3: /// the default value for that type is returns instead of throwing an exception4: /// 5: /// 6: /// 7: /// 8: private T safeCastTo<T>(object obj)9: {10: if (obj == null)11: {12: return default(T);13: }14: if (!(obj is T))15: {16: return default(T);17: }18: return (T)obj;19: ......

Posted On Wednesday, November 9, 2005 7:32 AM

Copyright © Eliot Ng | Powered by: GeeksWithBlogs.net