Tuesday, September 21, 2010

C# Tidbits : Coming from C++, the little things i noticed

[a brief comparison between C++, Java, VB and C# (Dot Net)]

Since I'm used to c++ (Borland BCB), java, even VB.. these are some things that i just found interesting in c# (only those simple/basic differences that i notice from time to time while developing in C#)..

voidmain

1. the main program is "static void Main()"
with the capital 'M'

2. everything should be put inside classes (just like in java)..
there are no global variables. the closest to a global variable is a "static" variable.

3. the native data types (integer, char, etc) are actually classes in c#.

4. there is an "Object" data type in c# similar to VB.
every class in C# is derived from this class (coming from the .Net library), and has inherited its functionality.

5. there is a "decimal" type in c#, commonly used for currencies.
i think it is bigger than double.

6. you can still use pointers, but every method that uses it should be preceded with the "unsafe" keyword - since it is considered unsafe (more on 'unmanaged' actually) in c#.

7. the "sizeof" operator is also considered unsafe, therefore precede every method that uses it with the "unsafe" keyword.

8. the "switch" statement requires that every case has the ending "break" keyword
(unlike in c++ where the code can 'fall-thru' the next case). there should always
be a "break" whenever a case contains at least one statement.
i.e. this is valid
case 1: x++; break;
case 2: y++; break;
i.e. this is also valid
case 1: //no statement at all, just fall-thru
case 2: x++; break;
case 3: y++; break;

9. c# also has a "goto" keyword, just like in VB or C++.
the goto can also be used to have the "fall-thru" effect  in c++ for a switch statement..
in fact, that is the only 'reasonable' use for "goto" in csharp (inside a switch statement).

10. use the "ref" or "out" keywords to pass-by-reference for methods.
the "ref" requires that the parameters should be initialized first.
the "out" doesn't, which is particularly useful when the function's main purpose is to
initialize a set of variables.
the definition and the call should both use the "ref" and the "out" keywords respectively.

11. you can use the "readonly" keyword to have a readonly accessible member variables.

12. the "this" operator cannot be used in a static method of a class, of course
since a static method does not have and need an instance.

13. for derived classes:
when overriding members or methods, the parents method still exists. to use them (within the derived class) use the "base" keyword, then access the members from there. to completely remove the use of a base member/method, override them but use the "new" keyword when declaring your new members/methods. by doing this, the parents members are replaced by the derived class' members.

14. there is an "Equals()" function for every class created (based from the overall
parent class of Object) and a static "Equals()" function that can compare two objects.   another helpful function is the "ToString()" function. For native types it works smoothly, for user-defined classes, please override it.

15. the (sort of) equivalent of a callback function or pointer-to-function from C++ is
the "delegate" declaration.

16. between class declarations, unlike in c++ but similar to java, there is no semicolon.

17. the "new" keyword automatically initializes a variable.
for classes, using the "new" keyword allots memory on the heap, but for simple/native types, the "new" keyword creates the object on the stack and calls the constructor.

18. you can directly instantiate/initialize a class’ member at it’s declaration.
This is really handy, since you don’t have to do all the initialization at your constructor (as i usually do – and still do).

19. no more code macros
What?! No more #defines? Dang! and i was so used to it.

There are still a lot of things i noticed (and still are) – which will make this list expand to kingdom-come. So, I won't be posting them all up.
These are the first few things that i noticed on my first few weeks in C# development. Hopefully, i could create sequels every now and then.

0 comments:

Post a Comment