Coffeehouse Post

Single Post Permalink

View Thread: Anders Interview next week: Ask Questions Here
  • User profile image

    why is there both explicit casting and as operator conversions in the language? 

        DerivedClass dc = new DerivedClass();
        BaseClass bc1 = (BaseClass)dc;
        BaseClass bc2 = dc as BaseClass;

    The distinction that one throws an exception when the object cannot be cast and the other returns null is kind of confusing. And then there is an "explicit operator" static method that can be coded to provide customized conversions. But that only works for casting, not for as operator conversions.