Sei sulla pagina 1di 4

PRIMITIVE DATA TYPES IN C#

int
uint
Byte
Sbyte
short
ushort
long
ulong

=>
=>
=>
=>
=>
=>
=>
=>

4 bytes For Integer Values =2^32


Unsigned 4 bytes For Integer Values
1 byte (0-255) =>2^8
Signed 1 byte
Signed 2 bytes for integer values =>2^16
Unsigned Signed 2 bytes for integer values
Signed 8 bytes for Integer Values
Unsigned 8 bytes for Integer Values

Float
Decimal
Double

=> 4 bytes Decimal Value


=> 8 Bytes Decimal Value
=> 8 bytes Decimal value

char
string

=> 2 bytes Character


=> 2 bytes per character in string

bool

=>Boolean values True/False

var

=> it has no specific datatypes. Whatever value


we assign to the Var, its datatype changes
eg: var x=10; X is of int type
var x="hi"; x is of string type
--------------------------------------------------//Array = Continous memory allocations of same type of data
//1-D Array
int[] a = new int[5];
Console.WriteLine("Enter 5 nos");
for (int i = 0; i <= 4; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Reverse Array Values=");
for (int i = 4; i >= 0; i--)
{
Console.WriteLine(a[i]);
}
Console.ReadKey();
---------------------------------------------------int[] a = new int[5];
Console.WriteLine("Enter 5 nos");
for (int i = 0; i <= 4; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter value to search");
int n = Convert.ToInt32(Console.ReadLine());
int j;
for ( j = 0; j <= 4; j++)
{
if (a[j] == n)
{
Console.WriteLine("Found=" + j);
break;
}

accordingly

}
if (j == 5)
Console.WriteLine("Not Found");
--------------------------------------------------------int[] a = new int[5];
Console.WriteLine("Enter 5 nos");
for (int i = 0; i <= 4; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
//Foreach loop only works on collections
//Automatically start 0 position, run upto last position &
//increement by 1
Console.WriteLine("Array Values are:-");
foreach (int i in a)
{
Console.WriteLine(i);
}
--------------------------------------------------------static void Main(string[] args)
{
//Array = Continous memory allocations of same type of data
//2-D Array
int[,] a = new int[3, 3];
Console.WriteLine("Enter 9 numbers");
int r,c;
for (r = 0; r <= 2; r++)
{
for (c = 0; c <= 2; c++)
{
a[r,c] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("The Values are:");
for (r = 0; r <= 2; r++)
{
for (c = 0; c <= 2; c++)
{
Console.Write(a[r, c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
--------------------------------------------------------------static void Main(string[] args)
{
//Array = Continous memory allocations of same type of data
//Jagged Array -It is an array of arrays of different sizes
int[][] j=new int[3][];
//Defining
j[0] = new
j[1] = new
j[2] = new

columns in each Row


int[3];
int[2];
int[4];

int i;
Console.WriteLine("Enter 3 Data in Row1");
for (i = 0; i <= 2; i++)
j[0][i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 2 Data in Row2");
for (i = 0; i <= 1; i++)
j[1][i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 4 Data in Row3");
for (i = 0; i <= 3; i++)
j[2][i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Values in Jagged Array");
for (i = 0; i <= 2; i++)
{
int[] a = j[i];
foreach (int k in a)
{
Console.Write(k+" ");
}
Console.WriteLine();
}
}
-------------------------------------------------------------------------------static void Main(string[] args)
{
//String in C#
Console.WriteLine("Enter a String");
String s1 = Console.ReadLine();
Console.WriteLine("Length of the String=" + s1.Length);
Console.WriteLine("String in UpperCase=" + s1.ToUpper());
string s2 = "Hello";
int i = s1.CompareTo(s2);
if (i == 0)
Console.WriteLine("Both Strings are of Same size");
else if (i < 0)
Console.WriteLine("1st String Smaller");
else
Console.WriteLine("2nd string smaller");
if (s1.Equals(s2))
Console.WriteLine("Both strings are same");
else
Console.WriteLine("Both strings are different");
bool r = s1.Contains("H");
if (r == true)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
string st = s1.Substring(2, 3);
Console.WriteLine("SubString="+st);

Potrebbero piacerti anche