wickedcoolthoughts
Wednesday, March 26, 2008
  .Net Type Safety
Working with the ternary(conditional) operator in C# I came across an interesting problem. The following piece of code does not compile.


class Cat{}
class Computer{}
......
Object obj = need_a_cat? new Cat():new Computer();
......


The above code fails with an error that " there is no implicit conversion between Cat and Computer". That is correct ;).However still wondering why is a type checking required in the above case.

On the contrary with enums where type checking is expected there is no type checking!


enum Day_Of_Week { sun, mon, tue ....}

String DoSomething(Day_Of_Week day) {
.......
}

....

DoSomething(Day_Of_Week.sun+109);



The above call DoSomething(Day_Of_Week.sun+109); should have failed during compilation. But it doesn't ;)
 

My Photo
Name:
Location: India

I am passionate about making Better Software Solutions,that helps businesses to stay agile, by applying Agile,Lean and Systems Thinking principles. Also I grow coffee,spices and in general has big interest in pesticide free food!

Archives
December 2007 / January 2008 / February 2008 / March 2008 / June 2011 /


Powered by Blogger

Subscribe to
Posts [Atom]