C# Data Types
C# is a strongly typed language. It means, that you cannot use variable without data types. Data types tell the compiler that which type of data is used for processing. Such as if you want to work with string value then you will have to assign string type variable to work with. C# provides two types of data types: Value types and Reference types.
A Value type data type stores copy of the value whereas the Reference type data types stores the address of the value. C sharp provides great range of predefined data types but it also gives the way to create user defined data types.
Value Types:
Data Types | Size | Values |
---|---|---|
sbyte | 8 bit | -128 to 127 |
byte | 8 bit | 0 to 255 |
short | 16 bit | -32,768 to 32,767 |
ushort | 16 bit | 0 to 65,535 |
int | 32 bit | -2,147,483,648 to 2,147,483,647 |
uint | 32 bit | 0 to 4,294,967,295 |
long | 64 bit | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | 64 bit | 0 to 18,446,744,073,709,551,615 |
char | 16 bit | 0 to 65535 |
float | 32 bit | -1.5 x 1045 to 3.4 x 1038 |
double | 64 bit | -5 x 10324 to 1.7 x 10308 |
decimal | 128 bit | -1028 to 7.9 x 1028 |
bool | — | True or false |
Reference Types:
Data Types | Size | Values |
---|---|---|
string | Variable length | 0-2 billion Unicode characters |
object | — | — |
Working with Variables
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.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Variable_Example { class Program { static void Main(string[] args) { //cretaing integer type variable int num1, num2, result; //Displaying message Console.WriteLine("Please enter first value"); //Accepting Value in num1 num1 = Int32.Parse(Console.ReadLine()); //Displaying message Console.WriteLine("Enter second Value"); //Accepting Value num2 = Int32.Parse(Console.ReadLine()); result = num1 + num2; //processing value 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);
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.
Conversion
C# accepts string value by default. If you are using other value then you will have to convert of specific data types.
num1 = Int32.Parse(Console.ReadLine());
You can use the following method to convert one data type to another data type.
Integer = int32.parse() or Convert.ToInt32() Float= (float) Double=Convert.ToDouble() Decimal=Convert.ToDecimal() Byte=Convert.ToByte()