Monday, March 26, 2007

Every Type is derived from...

A simple question...

Which is the type from which every other type is derived? Think before you look forward!
If you are answer is System.Object, then it's partially correct. The actual answer is System.Object if it's a reference type. If it is a value type then it derives from System.ValueType.

Saturday, March 24, 2007

Multiple Entry Points

A dot net program has a single point of entry (which is generally the Main function).
public static void Main(int args[])

A well-known fact:The execution of the program starts from here. If we look at the sig of the function we see that the Main function is publi. But ever wondered what happens if the sig is: private static void Main(int args[])?

The Main method is invoked from outside of the program. So it is, obviously, essential that the method be public. But would the program compile if it was private? The answer is: YES. It would compile. For some reason, not so obvious to me, the compiler/runtime doesn't see difference between a public and a private Main method. All it needs is a Main method.

So what if, in a program, we have two Main methods, either private or public or both? The compiler/runtime is confused. It doesn't know which is the actual point of entry. So, ambiguity! Hence, errors!

But what if for some reason you want to have two main methods? Can you make things work or is it the end of the road? It's definitely not the end. You can still make it work, with as many Main methods you want. Only condition being, the fully qualified name of the method (or the type containing it) should be different from each of the other Main methods.
namespace MainNamespace
class FirstMain
public static void Main(int args[]) {}

class SecondMain
public static void Main(int args[]) {}

To build this program use:
csc.exe Program.cs /main:MainNamespace.FirstMain

This will do the trick for you.
The next obvious question is how would we compile it using VS 2005? I would take this in my next post.

Friday, March 23, 2007

Software and Writing - A Bad Combination!!

For many know that I work in software but few know that I'm into writing too. And since I'm into Dot Net Framework, this is actually a real bad combination, for reasons that I discovered only yesterday.

Not that anything is wrong with software or writing. But both require me to be highly intellectual (of course, which I'm :-)). So my intellectuality is as follows:
class intellectuality
attention to detail; //for example

Now that I'm an intellectual, software and writing to me would be

class software : intellectuality

class writing : intellectuality

And I would become
class individual : software, writing
a clear example of multiple inheritence (Mind you! dot net framework doesn't allow this and "why" is what I'll try to address).

Now, I have a software class and a writing class defining me. And each of these two classes are, in turn, defined by one intellectuality class each. When we create an instance of "individual" class, instances of "writing" and "software" are implicitly created. And since writing derives from "intellectuality" an instance of "intellectuality" is also created. Same happens for software also. So, two intellectuality classes, one writing class and one software class define me.

Now where is all this (intellectuality) going to? Since all this, yet, doesn't make any sense, let's drill into it lil' more and see if this is of any use (otherwise you can always leave your brickbats in comments).

First problem here is, though I'm an individual with ability to code and write and strong intellectual skills, I'm being represented by two intellectual classes and not one. Mind you! I'm an intellect. i.e. Since "individual" derives from "writing" and "software", it should essentially have 3 base classes (writing, software and intellectuality) but it has 4 base classes (writing, software and two intellectuality classes).

Second, if I had to use my "attention to detail" ability from my "intellectuality", I do not know which one of the two intellectuality classes to make use of! Should I make use of intellectuality class of writing or of software? (personally I would prefer writing :-)) i.e. if we had to use a method or property of the base class "intellectuality" the runtime doesn't know from which instace to refer to (we have 2 instances created)!

Third, let's say I want to fill in my "intellectuality" with abilities in my "individual". So, I do
intellectuality intellect = new individual();
I'm essentially creating a base class as an instance of child class.
We have problems here. As we have already seen, "individual" ends up having two "intellectuality" sub classes and hence the compiler doesn't know which "intellectuality" object I'm trying to refer to!

These seem to be the "not so obvious" reasons for dot net framework banning Multiple Inheritence. This is also called as the "Diamond" problem. And as far as my life is concerned, it's far more simple and easier and, of course, devoid of "Diamond"(s)!!! :-) :-)

Thursday, March 22, 2007

An Obvious Mistake

class MyClass
private void Sample()
int i=10;
int i=20;

Will this code compile?
Obviously it wouldn't!!

But why wouldn't it compile?
Obviously because i has multiple declarations!!

But what is not so obvious is the reason behind not allowing multiple declarations. To talk about those not so obvious reasons, first of all one basic point. What happens when you say int i=10?

4 bytes of memory gets allocated, 10 is stored in it and the identifier 'i' points to it. Simple!

Now let's say we do multiple declarations for i, like in the above program. int i=10 allocates 4 bytes of memory and i points to it. Then we have int i=20. Another 4 bytes is allocated, 20 stored in it and, again, identifier 'i' points to this new memory. So when you use 'i' again anywhere, the compiler or runtime doesn't know which chunk of memory you are trying to reference to! Hence, undesired results!

This is the not so obvious reason for the program not compiling!