A variable refers to the memory address. When you create
variable, it creates holds space in the memory that is used for storing
temporary data. As
you know about c# data types, each data type has predefined size. In this page you
will learn how to
use data types to create variable.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
Working_with_Variables
{
class Program
{
static void Main(string[]
args)
{
//cretaing
integer type variable
int
num1, num2, result;
//Displaying
message
Console.WriteLine("Please enter 1st value");
//Accepting
Value in num1
num1 = Int32.Parse(Console.ReadLine());
//Displaying
message
Console.WriteLine("Please enter 2nd Value");
//Accepting
Value
num2 = Int32.Parse(Console.ReadLine());
result = num1 + num2; //processing value and adding
Console.WriteLine("Add of {0} and {1} is {2}", num1, num2,
result); //Output
Console.ReadLine();
}
}
}
In the preceding example we create three integer type
variable num1,num2 and result. num1 and num2 is used for accepting user value
and result is used for adding both number. The
new thing in the preceding example is number of placeholders.
Console.WriteLine("Add of {0} and {1} is {2}", num1, num2,
result); //Output
If you want to display more than
one variable values then you will
have to assign place holder for each variables. In the preceding line {0}
denotes to num1, {1} denotes to num2 and {2} denotes to result.
num1 = Int32.Parse(Console.ReadLine());
C# accepts string value by default. If you are using
other value then you will have to convert of specific data
types.
No comments:
Post a Comment