MSSql MySql Python javascript PHP java DotNet Ruby Hackme Takeawy Code Twitter Facebook

The house of developers, A website to classify and rank developers

 
 
 
 
Country Rank: 3
World Rank: 86
Profile Viewed: 203
Points: 4303
04 May 2010

C# Variables:Beginners

Okay, before we begin, there are a couple Important Notes regarding C# user-defined names (variables).
  • C# is case sensitive, so the variables: VarName, varname, and varName are three distinct variables.
  • A Variable may contain letters, numbers and underscores (spaces are not allowed)
  • A Variable must start with a _, @, or a letter.
  • It cannot start with a number and cannot be a C# keyword (unless it starts with a @ -- which we will discuss in further detail later on). For a list of C# Keywords, visit: http://msdn.microsoft.com/en-us/library/x53a06bb(VS.71).aspx
Typically, variable names use 'camelCase' - which means that the first word in the user defined name (identifier or variable) is all lowercase.  After that, the first letter of each word is in uppercase.  'anExampleOfCamelCasing'.
Additionally, Classes and Methods typically follow a slightly different convention: PascalCasing: In which the first word begins with an uppercase letter also. 'AnExampleOfPascalCasing'
Now in our previous HelloWorld program, we have seen a couple uses of our very first variables for input and output:
            int x = 123;
            string helloWorld = "Hello Again";

            Console.WriteLine("Hello C# Programming World!");
            Console.WriteLine(x);
            Console.WriteLine(helloWorld);

The first variable we created was an integer named 'x', which contained the number 123.  We then used the Console.WriteLine() Method to output the value of x to the console.
We then created a string variable, assigned it the value of 'Hello Again' and output the value to the console (*a string is technically a class - but for the purpose of this section, we will refer to it as a variable).  
Now we also mentioned that the general syntax to declare a variable in C# is: 
;
To initialize the variable (assign a value to it), the general syntax is:
= ;
When creating a variable that we already know the value for, we can combine them on one line such as:
= ;

Now should you have a program which uses the same value over and over and over, you can declare a Constant by using the const keyword such as:
const double PI = 3.14159;  //(NOTE that named constants typically appear ALL UPPERCASE.  This helps us differ them from otherr variables.

Why would we do that when we could just type 3.14159 whenever we need to refer to pi ?  Well, other than the obvious fact that 'PI' is shorter than '3.14159' by using the name 'PI', you would not make any errors as you might by entering in 3.14159

Another great example would be a program that calculates and generates bills for a customer.  Several places throughout the program, you would need to utilize a value for tax.  Now lets say that this in-depth program has the tax listed 100 different places throughout the program.  Suppose the value of tax changes, you would need to go through your program manually, and change every value.  If you use a constant, then you would only have to update the value of that constant, and the next time the program runs, it will calculate the bills with the correct tax rate.  When the program is compiled, it will automatically change the name of the constants to the values that they represent.

Now this syntax will work for any of the built-in C# Data Types:
C# Type .Net Framework type
bool          System.Boolean
byte          System.Byte
sbyte  System.SByte
char          System.Char
decimal  System.Decimal
double  System.Double
float          System.Single
int          System.Int32
uint          System.UInt32
long          System.Int64
ulong  System.UInt64
short          System.Int16
ushort  System.UInt16
*string        System.String
*With exception of string, this list would be considered a list of simple data types in C#.
For a list of C# Built-in data types and explainations, visit: http://msdn.microsoft.com/en-us/library/aa287910(VS.71).aspx

Next Section will further expand upon this while we are introduced to a way to accept user input in our Console program.
Facebook Share Delicious Share Digg Share Google Buzz Share My Space Share Reddit Share Stumbleupon Share Technorati Share Twitter Share

Comments

Please, login or register to add a comment.