Unit2 1 PDF
Unit2 1 PDF
C# is a type-safe language. Variables are declared as being of a particular type, and each variable is
constrained to hold only values of its declared type In C#, there are two types of data types are used:
1. Value Types
2. Reference types
A variable of value types directly contains only an object with the value.
A variable of reference type directly contains a reference to an object. Another variable may contain
a reference to the same object.
We can define our own value types by declaring enumerations or structures.
Example of Value Types is: int A = 50; here variable A hold value 50 that can be used in program.
We can define new reference types class, interface, and delegate declarations.
Example of reference type is:
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//sbyte
sbyte a = -10;
Console.WriteLine("sbyte...");
Console.WriteLine("a = " + a);
Console.WriteLine("type of variable = " + a.GetType());
Console.WriteLine("size of sbyte = " + sizeof(sbyte));
Console.WriteLine("Min value of sbyte = " + sbyte.MinValue);
Console.WriteLine("Max value of sbyte = " + sbyte.MaxValue);
Console.WriteLine();
//byte
byte b = 10;
Console.WriteLine("byte...");
Console.WriteLine("b = " + b);
Console.WriteLine("type of variable = " + b.GetType());
Console.WriteLine("size of byte = " + sizeof(byte));
Console.WriteLine("Min value of byte = " + byte.MinValue);
Console.WriteLine("Max value of byte = " + byte.MaxValue);
Console.WriteLine();
//char
char c = 'P';
Console.WriteLine("char...");
Console.WriteLine("c = " + c);
Console.WriteLine("type of variable = " + c.GetType());
Console.WriteLine("size of char = " + sizeof(char));
Console.WriteLine("Min value of char = " + (int)(char.MinValue));
Console.WriteLine("Max value of char = " + (int)(char.MaxValue));
Console.WriteLine();
//short
short d = -18910;
Console.WriteLine("short...");
Console.WriteLine("d = " + d);
Console.WriteLine("type of variable = " + d.GetType());
Console.WriteLine("size of short = " + sizeof(short));
Console.WriteLine("Min value of short = " + short.MinValue);
Console.WriteLine("Max value of short = " + short.MaxValue);
Console.WriteLine();
//ushort
ushort e = 18910;
Console.WriteLine("ushort...");
Console.WriteLine("e = " + e);
Console.WriteLine("type of variable = " + e.GetType());
Console.WriteLine("size of ushort = " + sizeof(short));
Console.WriteLine("Min value of ushort = " + ushort.MinValue);
Console.WriteLine("Max value of ushort = " + ushort.MaxValue);
Console.WriteLine();
//int
int f = -893818910;
Console.WriteLine("int...");
Console.WriteLine("f = " + f);
Console.WriteLine("type of variable = " + f.GetType());
Console.WriteLine("size of int = " + sizeof(int));
Console.WriteLine("Min value of int = " + int.MinValue);
Console.WriteLine("Max value of int = " + int.MaxValue);
Console.WriteLine();
//uint
int g = 893818910;
Console.WriteLine("uint...");
Console.WriteLine("g = " + g);
Console.WriteLine("type of variable = " + g.GetType());
Console.WriteLine("size of uint = " + sizeof(uint));
Console.WriteLine("Min value of uint = " + uint.MinValue);
Console.WriteLine("Max value of uint = " + uint.MaxValue);
Console.WriteLine();
//long
long h = -90909893818910;
Console.WriteLine("long...");
Console.WriteLine("h = " + h);
Console.WriteLine("type of variable = " + h.GetType());
Console.WriteLine("size of long = " + sizeof(long));
Console.WriteLine("Min value of long = " + long.MinValue);
Console.WriteLine("Max value of long = " + long.MaxValue);
Console.WriteLine();
//ulong
ulong i = 90909893818910;
Console.WriteLine("ulong...");
Console.WriteLine("i = " + i);
Console.WriteLine("type of variable = " + i.GetType());
Console.WriteLine("size of ulong = " + sizeof(ulong));
Console.WriteLine("Min value of ulong = " + ulong.MinValue);
Console.WriteLine("Max value of ulong = " + ulong.MaxValue);
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
C# bool keyword
In C#, bool is a keyword which is used to declare a variable that can store Boolean
values true or false. bool keyword is an alias of System.Boolean. It occupies 1 byte (8 bits) in the memory.
There are only two possible values that can be assigned to a bool variable 1) true or 2) false
using System;
using System.Text;
namespace Test
class Program
//printing value
Console.WriteLine("answer: " + answer);
Console.ReadLine();
C# byte keyword
In C#, byte is a keyword which is used to declare a variable that can store an unsigned value between 0 to
255. byte keyword is an alias of System.Byte. It occupies 1 byte (8 bits) in the memory.
Syntax:
C# sbyte keyword
In C#, sbyte is a keyword which is used to declare a variable that can store a signed value between the
range of -128 to +127. sbyte keyword is an alias of System.SByte.
Syntax:
C# char keyword
In C#, char is a keyword which is used to declare a variable that can store a character value (Unicode value)
value between the range of +U0000 to +UFFFF. char keyword is an alias of System.Char.
Syntax:
char variable_name = value;
It can store the character between the range of Unicode +U0000 to +UFFFF.
C# decimal keyword
In C#, decimal is a keyword which is used to declare a variable that can store a floating type value (value
with the precision) between the range of ±1.0 x 10-28 to ±7.9228 x 1028. decimal keyword is an alias
of System.Decimal.
It can store the value between the range of ±1.0 x 10-28 to ±7.9228 x 1028.
C# float keyword
In C#, float is a keyword which is used to declare a variable that can store a floating point value between
the range of ±1.5 x 10−45 to ±3.4 x 1038. float keyword is an alias of System.Single.
C# double keyword
In C#, float is a keyword which is used to declare a variable that can store a floating point value between
the range of ±1.5 x 10−45 to ±3.4 x 1038. float keyword is an alias of System.Single.
Note: Any floating point value without using any suffix is considered as a double value. We can also
specify a suffix d or D to represent a double value.
C# int keyword
In C#, int is a keyword which is used to declare a variable that can store an integral type of value (signed
integer) the range of -2,147,483,648 to 2,147,483,647. int keyword is an alias of System.Int32.
C# uint keyword
In C#, uint is a keyword which is used to declare a variable that can store an integral type of value
(unsigned integer) the range of 0 to 4,294,967,295. uint keyword is an alias of System.UInt32.
C# uint keyword
In C#, long is a keyword which is used to declare a variable that can store a signed integer value between
the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. long keyword is an alias
of System.Int64.
C# ulong keyword
In C#, ulong is a keyword which is used to declare a variable that can store an unsigned integer value
between the range of 0 to 18,446,744,073,709,551,615. ulong keyword is an alias of System.UInt64.
C# short keyword
In C#, short is a keyword which is used to declare a variable that can store a signed integer value between
the range of -32,768 to 32,767. short keyword is an alias of System.Int16.
C# ushort keyword
In C#, ushort is a keyword which is used to declare a variable that can store an unsigned integer value
between the range of 0 to 65,535. ushort keyword is an alias of System.UInt16.
Enumeration (enum) in C#
enum stands for enumeration, it is a set of named integer constants, their default integer values start with
0, we can also set any other sequence of the values.
C# void keyword
In C#, void is a keyword. a void is a reference type of data type, it is used to specify the return type of a
method in C#.
Syntax:
Eg
using System;
using System.Text;
namespace Test
{
class Example
{
public void printText()
{
Console.WriteLine("Hello world!");
}
public void sum(int a, int b)
{
Console.WriteLine("sum = " + (a + b));
}
};
class Program
{
static void Main(string[] args)
{
//calling functions
Example ex = new Example();
ex.printText();
ex.sum(10, 20);
C# var keyword
In C#, var is a keyword, it is used to declare an implicit type variable, which specifies the type of a variable
based on initialized value.
eg
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10.23;
var c = 10.23f;
var d = 10.23m;
var e = 'X';
var f = "Hello";
Command line argument is very important concept in every programming language. Using this we can
pass arguments to main function at runtime.
Eg.
using System;
class Sample
{
static void Main(string[] args)
{
int SUM = 0;
int X = 0;
int Y = 0;
X = Convert.ToInt32(args[0]);
Y = Convert.ToInt32(args[1]);
SUM = X + Y;
Console.WriteLine("Sum is: "+SUM);
}
}
Example:
using System;
using System.Text;
namespace Test
//structure
//method
this.name = name;
this.age = age;
this.perc = perc;
};
//class
//default constructor
public student_2()
this.name = "N/A";
age = 0;
perc = 0.0f;
//method
this.name = name;
this.age = age;
this.perc = perc;
};
class Program
{
static void Main(string[] args)
std1.dispValues();
//setting values
std1.dispValues();
Console.WriteLine();
std2.dispValues();
//setting values
std2.dispValues();
//hit ENTER to exit
Console.ReadLine();