Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0012 Variable

A variable stores value and variable type defines the behaviours of the variable. The following code uses the int type variable to calculate the result of 2 plus 2. using System; class Program { static void Main(string[] args) { int i = 2; int j = 2; int result = i + j; Console.WriteLine("result=" + result); } } The result: result=4 In the code above, i and j are two variables. The + (adding) is the behaviour. int is a predefined data type in C#. Another predefined data type is string. You can store a list of characters in the string type variable. using System; class Program { static void Main(string[] args) { string str = "demo from rntsoft.com"; Console.WriteLine("str=" + str); } } The result: str=demo from rntsoft.com The string type variable can be converted to its upper case form: using System; class Program { static void Main(string[] args) { string str = "demo from rntsoft.com"; Console.WriteLine("str=" + str.ToUpper()); } } The output: str=DEMO FROM RNTSOFT.COM