Page 12 - DCAP505_MODERN_PROGRAMMING_TOOLS_AND_TECHNIQUES_II
P. 12
Modern Programming Tools & Techniques – II
Notes The second most annoying thing about working in C and C++ is figuring out exactly what type of
data type to use. In C#, a Unicode character is no longer a wchar_t, it’s a char. A 64-bit integer is
a long, not an __int64. And a char is a char is a char. There’s no more char, unsigned char, signed
char, and wchar_t to track.
The third most annoying problem that you run across in C and C++ is integers being used as
Booleans, causing assignment errors when you confuse = and ==. C# separates these two types,
providing a separate bool type that solves this problem. A bool can be true or false, and can’t be
converted into other types. Similarly, an integer or object reference can’t be tested to be true or
false - it must be compared to zero (or to null in the case of the reference). If you wrote code like
this in C++:
int i;
if (i) . . .
You need to convert that into something like this for C#:
int i;
if (i != 0) . . .
Another programmer-friendly feature is the improvement over C++ in the way switch statements
work. In C++, you could write a switch statement that fell through from case to case.
For example, this code
switch (i)
{
case 1:
FunctionA();
case 2:
FunctionB();
Break;
}
would call both FunctionA and FunctionB if i was equal to 1. C# works like Visual Basic, putting
an implied break before each case statement. If you really do want the case statement to fall
through, you can rewrite the switch block like this in C#:
switch (i)
{
case 1:
FunctionA();
goto case 2;
case 2:
FunctionB();
Break;
}
6 LOVELY PROFESSIONAL UNIVERSITY