ideas | discover | explore

Monday, August 29, 2011

C# 2 – Variables

C# 2 - Variables

1.  Create a new C# console project.
2.  Inside main() type the following code.

string myName = "Reymon Reyes"//Put your name within the two double quotes.
int myAge = 25;
float myHeight = 162.62F;
decimal myWeight = 132.28M;

Console.WriteLine("My profile");
Console.WriteLine("Name: {0}", myName);
Console.WriteLine("Age: {0}", myAge);
Console.WriteLine("Height: {0}", myHeight);
Console.WriteLine("Weight: {0}", myWeight);

Console.WriteLine("Press any key to continue.....");
Console.ReadLine();



3. Your Program.cs will now look like this.


4. Press F5 to run your code.





  • Syntax for declaring a variable.
    • <data type> <variable name>;
    • Eg: int myAge;
  • Data type can be any of the primitive types or a class.
  • Variable names cannot begin with a number or any special characters although an underscore is allowed.
  • Variable names are case sensitive which means myAge is different from MyAge.
  • Variable names cannot be any of the reserved keywords used within C#.
  • The declaration decimal myWeight = 132.28M; contains a suffix literal M to specify the value 132.28 as decimal type.

No comments: