Saturday, April 18, 2009

Instatiating an Interface

Recently, I found out that there is a way for “Instantiating an interface” in C#. Here is the code.

[ComImport]
[Guid("DC1CB768-0BE5-4200-8D0A-C844BFBE3DE7")]
[CoClass(typeof(Foo))]
interface IFoo
{
}
class Foo : IFoo
{
}

static void Main(string[] args)
{
    // Instatiating an Interface --- This works
    IFoo foo = new IFoo();
}

Please let me know your comments.

Tuesday, March 17, 2009

Interesting questions – Expert answers Part 2

Here are some of the questions I asked to Krzysztof Cwalina (Reusable frameworks design guru & PM@Microsoft) on 17 March 2009.


Question (Gopala): In C#, why don’t we have a feature called “Extension properties” just like extension methods ? This feature is available in F#.

I got a scenario during my development to add an extra property for System.IO.FileInfo class (like for example File Id)

Answer(Krzysztof): It was just harder to add this feature to the language. Maybe at some point in the future.

Question (Gopala): How to find out the size of a reference type (preferably with nested reference types)? Is there any operator like sizeof which calculates the size of a reference type?

Answer(Krzysztof): No, there is no easy way. Reflection is the only one I know. Just watch for performance

Question (Gopala): Why C# doesn’t allow method level static variables? Is there any alternative approach to use method level static variables in C# just like in C++?

Answer(Krzysztof): I don’t know/remember this design decision

Question (Gopala): As we know the only .NET language where we can use global variables is CIL …why global variables are not allowed in C#?

Answer(Krzysztof): Public static variables are global

Question (Gopala): In your design guidelines you have mentioned not to catch exceptions of type “Exception” (i.e. Generic exceptions) and instead we have to catch exceptions of specific type. This is a good practice but I have seen lots of products (including Microsoft products) don’t follow this guideline.

Some of the products I was talking about is:

1. Microsoft enterprise library exception handling application block

Answer(Krzysztof): Yes, this is a special case of a library for handling and logging exceptions.

2. Most of the code samples from MSDN

Answer(Krzysztof): This is good point. I don’t think the samples should do it. I think they do it for simplicity, but samples should show good practices.

3. Some products(Applications) which are developed in our company.

Answer(Krzysztof): Applications might sometimes want handle Exception. The FDGs are for libraries

Question (Gopala): I heard that initially interfaces were used in the design of ASP.NET provider model and later on most of the interfaces were replaced with abstract classes. What is reason behind this core design change?

Answer(Krzysztof): This is described in the Framework Design Guidelines.

Though I asked lot many questions I remember only these ones.
Guys, Try to answer my questions or Feel free to put comments\ suggestions.