Tuesday, September 28, 2004

Re: C# ?

The DS module in Cdac is over and I just flunked in the Mgpt. The next module is C and C++ programming and so I should have lots of questions coming up soon.

Sorry if you've told me this before already but what's Mgpt? So are they going to teach you C/C++ in this next module or just use it for your programming?

Just for a change I got a book from the library which I plan to go through very fast just for an overview - C# Progamming (with the public beta) by Wrox. So got another bunch of questions...

I have a super huge grin on me face. You don't know how happy this made me. Change is good :-)

Enums in c# are value types. So firstly are data types stored on the stack? And even class instances can be stored on the stack.. so what's the syntax for that?

In .NET there are two sets of objects... Reference and Value types. Reference types are Classes, Interfaces, Delegates, Arrays. Value types are Structs and Enums. Basically, all objects in the system have System.Object as the root object. But value types, also derive from System.ValueType. So you CANNOT create class instances on the stack. You can only create struct instances on the stack, which implicitly derive from System.ValueType. And btw, all primitive types in C# like int, double etc... are structs in the BCL (Base Class Library).

On enums i wanted to compare them with java enums. In java 5.0, enums were added which are actually classes. So when you say enum {something} you are actually extending a class. So enums in java are pretty advanced. I'll have to read more to give more info. What about c#? You can get more info on Enums from this Oreilly sample chapter

I guess again the motivation for making enums classes was for backward/forward compatibility. I scanned the article link you sent briefly and you're right... java enums have much more ability since they are full blown classes. Personally, I don't see the point of this. Enums play a very specific role in C++ and C#. They are just defining type-safe constants as a group. Why complicate matters by allowing it to do everything a class can do? Why would you want to define methods on an enum? Dyou see a benefit?

Another thing was the huge number of keywords. I read on const and readonly. Are they really required over final?

I dunno the exact number, but I guess C# must have maybe +20 keywords over java.

Regarding const and readonly - they serve two different purposes. const is evaluated at compile time and is implicitly static. So every instance of the class gets only one copy. readonly is evaluated at runtime and are not static. So every instance gets its own copy.

If you make any fields const in your app, when you compile it, the compiler will replace all the fields with the actual value. For ex.

class Math
{
    public const double PI = 3.14;
}

class Circle
{
    private double circumference( double radius )
    {
        return 2 * Math.PI * radius;
    }
}

Here, it will turn into 2 * 3.14 * radius. The compiler can make this optimization. You can't do it for readonly fields since they are evaluated at runtime.

But readonly fields have a different benefit. What if you shipped this code and then later on realized that PI is wrong or you want to make it more accurate. If you had made it a readonly field, you can just make the change and ship the updated Math class. Your Circle class's Circumference method will automatically use the new updated PI value without re-compiling. Since PI is evaluated at runtime it will automatically use the updated value. But since it's const here, you will have to recompile.

Does final do both?

The main question that propmted this blog was inheritance.virtual override new sealed and abstract... wow so many keywords and complex relationshipsDo you make use of all these keywords?? Isn't the Java style much more simple? Trying to appease c++ programmers, but added awhole extra bunch of words!!

Again, all those keywords serve a purpose just as const vs readonly. They didn't just get keyword happy. I had written a blog regarding polymorphism in C++, Java and C# long back - http://codeword.blogspot.com/2003/12/polymorphism-c-vs-java-vs-c.html. It talks about virtual, override and new. So check it out and see if you think they serve some meaningful purpose.

Java uses one keyword which can be used on classes, methods and fields. They have the same essential meaning for all - preventing things from being changed. C# has taken a different route. They make all methods non-changeable by default and then make you explicitly say if you want to change something. They requires more keywords.

On boxing and unboxing, why the need for a different object class from the default Object class? In Java the wrapper classes for primitives like Integer for int can be cast to Object. Wrt Java 5.0 I think the compiler will be generating casting code which was generally written by hand.

One thing you should understand is that all the types are defined in the .NET BCL. The language itself does NOT define any types. The languages only provide an alias for primitive, string and object types. So in C#, int == System.Int32, long == System.Int64, double == System.Double, string == System.String, object == System.Object etc... Same way VB and Managed C++ provide something similar. So there is no different object class from the default Object class.

So hope that cleared some stuff up. What are your impressions of .NET from the book. I've been blogging a lot about this stuff so you should be familiar with some of it already - atleast I hope.

Btw, check this link out - C# from a Java Developers Perspective. It's a bit old but quite comprehensive and an easy read. He'll have to update it for Tiger. And before you expect some .NET bashing... he's a Microsoft employee.

No comments: