|
|
In the previous lesson we learned about the different kinds of data types, that are available in C#. In this lesson we will continue our discussion on the primitive data types of C# programming
language. Their usage is pretty straight forward and the same as you are used to in C++ and Java. However you also need to know the following:
- Unlike C++, in C#, Boolean values do not convert to integers and Boolean values (true, false) do not equate to integer variables. Thus, you may not write:
if ( someFuncWhichReturnsAnIntegerValue() )
- Like Java, C# supports implicit widening conversion only. For narrowing conversion, for example from float to int, the programmer has to explicitly state his intentions.
- We we declared a variable in C and C++, it contained a random value until we assigned a value to it. This was a big source of bugs in C and C++ programs, where we accidentally tried to use
a variable without first assigning it a valid value. Fortunately C# does not allow this. You must assigne a valid value to a variable before trying to use the same, or your program would not
compile. This requirement or rule is also called Definite Assignment Rule.
C# Variable naming conventions:
- You should not start a variable name with an underscore(_).
- You should not create variable names that differ only by case. For example, it is not recommended to create name and Name as variables, simulteneously in a piece of code. It is confusing, too.
- You should start the variable name with a lower case letter and in a multiword variable name start the second and each subsequent word with an uppercase letter. This is also called
camelCase notation.
Please note that the above are just recommendations, but it is good if you follow them, as many experienced C# programmers do. The first two are compulsory only if you are writing programs that
are to interoperate with other .NET languages, such as Microsoft Visual Basice.NET.
Next >>> Lesson No. 7: C# Loops
|
|