0% found this document useful (0 votes)
321 views

Data Types in C

C# has two categories of data types - value types and reference types. Value types store data directly while reference types store references to data. There are predefined and user-defined data types. Predefined types include integral types like int and float that have set ranges and sizes. Reference types include class and interface types that are used to define custom types.

Uploaded by

habiba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
321 views

Data Types in C

C# has two categories of data types - value types and reference types. Value types store data directly while reference types store references to data. There are predefined and user-defined data types. Predefined types include integral types like int and float that have set ranges and sizes. Reference types include class and interface types that are used to define custom types.

Uploaded by

habiba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Data types in C#

Learn: C# programming data types: how many types of data types available in C#?
Which are the data types are in Value Types category and which are in Reference Types?

Data types define what and how data should be stored and used. 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.

C# predefined data types


The data types which are predefined in the C# compiler, these are also referred to
as Basic Data Type of C#. The value range (minimum and maximum values) and size of
these data types are predefined; anyone can not modify the range and size of these
data types.

Here is the list of basic/predefined C# data types with type, size, range etc
Image source: http://apprize.info/c/essential/2.html

 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:
 object ABC = new object();
 ABC.myValue = 50;
 Escape Sequences and Verbatim Strings are used in C#
string X = "\"Hello \nHow are you\"";
Here \n is used for print new line and \" is used to print double quote on
console screen.

Integral types of Data types in C#


C# integral type of data types: Here, we are going to learn about the integral types
of data types in C# with their system types, occupied size, value range and value types.
Submitted by IncludeHelp, on March 07, 2019

Here is the list of the built-in integral types of data types in


C#, sbyte, byte, char, short, ushort, int, uint, long and ulong

C# Integral types of Data types


Type System type Size (in Value Range Value type
bits)

sbyte System.SByte 8-bits -128 to 127 Signed integer

byte System.Byte 8-bits 0 to 255 Unsigned


integer

char System.Char 16-bits U+0000 to U+ffff Unicode


character

short System.Int16 16-bits -32,768 to 32,767 Signed integer

ushor System.Int16 16-bits 0 to 65,535 Unsigned


t integer

int System.Int32 32-bits -2,147,483,648 to 2,147,483,647 Signed integer

uint System.Int32 32-bits 0 to 4,294,967,295 Unsigned


integer
long System.Int64 64-bits -9,223,372,036,854,775,808 to Signed integer
9,223,372,036,854,775,807

ulong System.Int64 64-bits 0 to 18,446,744,073,709,551,615 Unsigned


integer

Example:

In this example, we are declaring variables of different integral types of data types,


initializing with the different value, printing the system types of the variables, size of the
types and min, max values of the types.

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();
}
}
}
ADVERTISEMENT

Output
sbyte...
a = -10
type of variable = System.SByte
size of sbyte = 1
Min value of sbyte = -128
Max value of sbyte = 127

byte...
b = 10
type of variable = System.Byte
size of byte = 1
Min value of byte = 0
Max value of byte = 255

char...
c = P
type of variable = System.Char
size of char = 2
Min value of char = 0
Max value of char = 65535

short...
d = -18910
type of variable = System.Int16
size of short = 2
Min value of short = -32768
Max value of short = 32767

ushort...
e = 18910
type of variable = System.UInt16
size of ushort = 2
Min value of ushort = 0
Max value of ushort = 65535

int...
f = -893818910
type of variable = System.Int32
size of int = 4
Min value of int = -2147483648
Max value of int = 2147483647

uint...
g = 893818910
type of variable = System.Int32
size of uint = 4
Min value of uint = 0
Max value of uint = 4294967295
long...
h = -90909893818910
type of variable = System.Int64
size of long = 8
Min value of long = -9223372036854775808
Max value of long = 9223372036854775807

ulong...
i = 90909893818910
type of variable = System.UInt64
size of ulong = 8
Min value of ulong = 0
Max value of ulong = 18446744073709551615

bool keyword in C#
C# bool keyword: Here, we are going to learn about the bool keyword in C#, what is
bool keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

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.

Syntax:

bool variable_name = value;

There are only two possible values that can be assigned to a bool variable 1) true or
2) false

C# code to demonstrate example of bool keyword


Here, we are declaring a bool variable answer, initializing it with the value true and
printing its value, type, and size of a bool type variable.
using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//bool variable declaration
bool answer = true;

//printing value
Console.WriteLine("answer: " + answer);
//printing type of variable
Console.WriteLine("Type of answer: " +
answer.GetType());
//printing size of a bool
Console.WriteLine("Size of a bool variable: " +
sizeof(bool));

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

answer: True
Type of answer: System.Boolean
Size of a bool variable: 1

byte keyword in C#
C# byte keyword: Here, we are going to learn about the byte keyword in C#, what is
byte keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

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:

byte variable_name = value;

It can store value between 0 to 255.

C# code to demonstrate example of byte keyword


Here, we are declaring a byte variable num, initializing it with the value 120 and printing
its value, type, and size of a byte type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//byte variable declaration
byte num = 120;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size of a bool
Console.WriteLine("Size of a byte variable: " +
sizeof(byte));

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output
num: 120
Type of num: System.Byte
Size of a byte variable: 1

sbyte keyword in C#
C# sbyte keyword: Here, we are going to learn about the sbyte keyword in C#, what is
sbyte keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

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.

It occupies 1 byte (8 bits) in the memory.

Syntax:

sbyte variable_name = value;

It can store the value between the range of -128 to +127.

C# code to demonstrate example of sbyte keyword


Here, we are declaring a sbyte variable num, initializing it with the value -120 and
printing its value, type and size of a sbyte type variable.

using System;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//char variable declaration
sbyte num = -120;
//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size of a decimal
Console.WriteLine("Size of a sbyte variable: " +
sizeof(sbyte));

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: -120
Type of num: System.SByte
Size of a sbyte variable: 1

char keyword in C#
C# char keyword: Here, we are going to learn about the char keyword in C#, what is
char keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

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 U+FFFF. char keyword is
an alias of System.Char.

It occupies 2 bytes (16 bits) in the memory.

Syntax:

char variable_name = value;

It can store the character between the range of Unicode +U0000 to +UFFFF.
C# code to demonstrate example of char keyword
Here, we are declaring a char variable chr, initializing it with the value 'X' and printing
its value, type and size of a char type variable.

using System;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//char variable declaration
char chr = 'X';

//printing value
Console.WriteLine("chr: " + chr);
//printing type of variable
Console.WriteLine("Type of chr: " + chr.GetType());
//printing size of a bool
Console.WriteLine("Size of a char variable: " +
sizeof(char));

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

chr: X
Type of chr: System.Char
Size of a char variable: 2

decimal keyword in C#
C# decimal keyword: Here, we are going to learn about the decimal keyword in C#,
what is decimal keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

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 occupies 16 bytes (128 bits) in the memory.

Note: To represent a decimal value, we use m or M as a suffix with the literal.

Syntax:

decimal variable_name = value;

It can store the value between the range of ±1.0 x 10 -28 to ±7.9228 x 1028.

C# code to demonstrate example of decimal keyword


Here, we are declaring a decimal variable num, initializing it with the
value 5610.2361m and printing its value, type and size of a decimal type variable.

using System;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//char variable declaration
decimal num = 5610.2361m;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size of a decimal
Console.WriteLine("Size of a decimal variable: " +
sizeof(decimal));

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 5610.2361
Type of num: System.Decimal
Size of a decimal variable: 16

float keyword in C#
C# float keyword: Here, we are going to learn about the float keyword in C#, what is
float keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 4 bytes (32 bits) space in the memory.

Note: To represent a float value, we use a suffix f or F with the value.

Syntax:

float variable_name = value;

C# code to demonstrate example of float keyword


Here, we are declaring a float variable num, initializing it with the value 12345.6789f and
printing its value, type and size of a float type variable.
using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
float num = 12345.6789f;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a float variable: " +
sizeof(float));
//printing minimum & maximum value of float
Console.WriteLine("Min value of float: " +
float.MinValue);
Console.WriteLine("Max value of float: " +
float.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345.68
Type of num: System.Single
Size of a float variable: 4
Min value of float: -3.402823E+38
Max value of float: 3.402823E+38

double keyword in C#
C# double keyword: Here, we are going to learn about the double keyword in C#,
what is double keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 8 byte (64 bits) space in the memory.

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.

Syntax:

double variable_name = value;

C# code to demonstrate example of double keyword


Here, we are declaring a double variable num, initializing it with the
value 12345.6789 and printing its value, type, and size of a double type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
double num = 12345.6789;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a double variable: " +
sizeof(double));
//printing minimum & maximum value of double
Console.WriteLine("Min value of double: " +
double.MinValue);
Console.WriteLine("Max value of double: " +
double.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345.6789
Type of num: System.Double
Size of a double variable: 8
Min value of double: -1.79769313486232E+308
Max value of double: 1.79769313486232E+308

int keyword in C#
C# int keyword: Here, we are going to learn about the int keyword in C#, what is int
keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 4 bytes (32 bits) space in the memory.

Syntax:

int variable_name = value;

C# code to demonstrate example of int keyword


Here, we are declaring an int variable num, initializing it with the value 12345 and
printing its value, type, and size of an int type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
int num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a int variable: " +
sizeof(int));
//printing minimum & maximum value of int
Console.WriteLine("Min value of int: " + int.MinValue);
Console.WriteLine("Max value of int: " + int.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345
Type of num: System.Int32
Size of a int variable: 4
Min value of int: -2147483648
Max value of int: 2147483647

uint keyword in C#
C# uint keyword: Here, we are going to learn about the uint keyword in C#, what is
uint keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 4 bytes (32 bits) space in the memory.

Syntax:

uint variable_name = value;

C# code to demonstrate example of uint keyword


Here, we are declaring a uint variable num, initializing it with the value 12345 and
printing its value, type and size of a uint type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
uint num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a uint variable: " +
sizeof(uint));
//printing minimum & maximum value of uint
Console.WriteLine("Min value of uint: " +
uint.MinValue);
Console.WriteLine("Max value of uint: " +
uint.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345
Type of num: System.UInt32
Size of a uint variable: 4
Min value of uint: 0
Max value of uint: 4294967295

long keyword in C#
C# long keyword: Here, we are going to learn about the long keyword in C#, what is
long keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 8 bytes (64 bits) space in the memory.

Syntax:

long variable_name = value;

C# code to demonstrate example of long keyword


Here, we are declaring a long variable num, initializing it with the value 12345 and
printing its value, type and size of a long type variable.
using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
long num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a long variable: " +
sizeof(long));
//printing minimum & maximum value of long
Console.WriteLine("Min value of long: " +
long.MinValue);
Console.WriteLine("Max value of long: " +
long.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345
Type of num: System.Int64
Size of a long variable: 8
Min value of long: -9223372036854775808
Max value of long: 9223372036854775807

ulong keyword in C#
C# ulong keyword: Here, we are going to learn about the ulong keyword in C#, what
is ulong keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019
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.

It occupies 8 bytes (64 bits) space in the memory.

Syntax:

ulong variable_name = value;

C# code to demonstrate example of ulong keyword


Here, we are declaring an ulong variable num, initializing it with the value 12345 and
printing its value, type and size of an ulong type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
ulong num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a ulong variable: " +
sizeof(ulong));
//printing minimum & maximum value of ulong
Console.WriteLine("Min value of ulong: " +
ulong.MinValue);
Console.WriteLine("Max value of ulong: " +
ulong.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345
Type of num: System.UInt64
Size of a ulong variable: 8
Min value of ulong: 0
Max value of ulong: 18446744073709551615

short keyword in C#
C# short keyword: Here, we are going to learn about the short keyword in C#, what is
short keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 2 bytes (16 bits) space in the memory.

Syntax:

short variable_name = value;

C# code to demonstrate example of short keyword


Here, we are declaring a short variable num, initializing it with the value 12345 and
printing its value, type and size of a short type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
short num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a short variable: " +
sizeof(short));
//printing minimum & maximum value of short
Console.WriteLine("Min value of short: " +
short.MinValue);
Console.WriteLine("Max value of short: " +
short.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

num: 12345
Type of num: System.Int16
Size of a short variable: 2
Min value of short: -32768
Max value of short: 32767

ushort keyword in C#
C# ushort keyword: Here, we are going to learn about the ushort keyword in C#, what
is ushort keyword, how to use it in C#?
Submitted by IncludeHelp, on March 07, 2019

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.

It occupies 2 bytes (16 bits) space in the memory.

Syntax:

ushort variable_name = value;

C# code to demonstrate example of ushort keyword


Here, we are declaring an ushort variable num, initializing it with the value 12345 and
printing its value, type and size of an ushort type variable.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//variable declaration
ushort num = 12345;

//printing value
Console.WriteLine("num: " + num);
//printing type of variable
Console.WriteLine("Type of num: " + num.GetType());
//printing size
Console.WriteLine("Size of a ushort variable: " +
sizeof(ushort));
//printing minimum & maximum value of ushort
Console.WriteLine("Min value of ushort: " +
ushort.MinValue);
Console.WriteLine("Max value of ushort: " +
ushort.MaxValue);

//hit ENTER to exit


Console.ReadLine();
}
}
}
Output

num: 12345
Type of num: System.UInt16
Size of a ushort variable: 2
Min value of ushort: 0
Max value of ushort: 65535

Enumeration (enum) in C#
C# Enum: Here, we are going to learn about the enum in C#, how to declare, access
enumeration in C#?
Submitted by IncludeHelp, on February 12, 2019

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.

An enum is defined by using the enum keyword.

Syntax:

enum enum_name {enumeration_list };

Example 1:

Here, we are defining an enum named colors with the three


constants RED, GREEN and BLUE, we are not initializing them with any value. Thus,
constant will have values 0, 1 and 2.

using System;
using System.Text;

namespace Test
{
class Program
{
//declaring enum
enum colors { RED, GREEN, BLUE };
static void Main(string[] args)
{
//printing values
Console.WriteLine("Red: {0}", (int)colors.RED);
Console.WriteLine("Green: {0}", (int)colors.GREEN);
Console.WriteLine("Blue: {0}", (int)colors.BLUE);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Red: 0
Green: 1
Blue: 2

void keyword in C#
C# void keyword: Here, we are going to learn about the void keyword in C#, what is
void keyword, how to use it n C#?
Submitted by IncludeHelp, on March 07, 2019

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#.

void keyword is an alias of System.Void.

Note: If there is no parameter in a C# method, void cannot be used as a parameter.

Syntax:

public void function_name([parameters])


{
//body of the function
}

C# code to demonstrate example of void keyword


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);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Hello world!
sum = 30
var keyword in C#
C# var keyword: Here, we are going to learn about the var keyword in C#, what is var
keyword, how to use it n C#?
Submitted by IncludeHelp, on March 07, 2019

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.

Syntax:

var variable_name = value;

C# code to demonstrate example of var keyword


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";

Console.WriteLine("value of a {0}, type {1}", a,


a.GetType());
Console.WriteLine("value of b {0}, type {1}", b,
b.GetType());
Console.WriteLine("value of c {0}, type {1}", c,
c.GetType());
Console.WriteLine("value of d {0}, type {1}", d,
d.GetType());
Console.WriteLine("value of e {0}, type {1}", e,
e.GetType());
Console.WriteLine("value of f {0}, type {1}", f,
f.GetType());

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

value of a 10, type System.Int32


value of b 10.23, type System.Double
value of c 10.23, type System.Single
value of d 10.23, type System.Decimal
value of e X, type System.Char
value of f Hello, type System.String

Differences between byte and sbyte in C#


C# byte and sbyte: Here, we are going to learn about the differences between byte
and sbyte in C#.
Submitted by IncludeHelp, on February 09, 2019

C# byte and C# sbyte


A single byte can store 8-bits value. Both are used for byte type of data i.e. the data that
contains an only 1-byte value.

byte is used to work with unsigned byte data, it works with an only positive value
between in the range of 0 to 255.

sbyte is used to work with the signed byte data, it works with both types of data
(Negative and Positive), it can store the between in the range of -128 to 127.

Differences between 'byte' and 'sbyte'


byte sbyte

byte stands for unsigned byte. sbyte stands for signed byte.


It can store positive bytes only. It can store negative and positive bytes.

It takes 8-bits space in the memory. It also takes the 8-bits in the memory.

Its data range is 0 to 255 that means it can store a Its data range is -128 to 127 that means it can store
minimum value 0 and maximum upto 255. minimum value -128 and maximum upto 127.

Declaration syntax: Declaration syntax:


byte variable; sbyte variable;

Example:

Declaring signed and unsigned byte variable, assigning them with the values and
printing the values.

using System;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
byte a;
sbyte b;

//printing minimum & maximum values


Console.WriteLine("Minimum value of byte: " +
byte.MinValue);
Console.WriteLine("Maximum value of byte: " +
byte.MaxValue);

Console.WriteLine("Minimum value of sbyte: " +


sbyte.MinValue);
Console.WriteLine("Maximum value of sbyte: " +
sbyte.MaxValue);
a = 0;
Console.WriteLine("a = " + a);
a = 255;
Console.WriteLine("a = " + a);

b = -100;
Console.WriteLine("b = " + b);

b = 123;
Console.WriteLine("b = " + b);

b = 127;
Console.WriteLine("b = " + b);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Minimum value of byte: 0


Maximum value of byte: 255
Minimum value of sbyte: -128
Maximum value of sbyte: 127
a = 0
a = 255
b = -100
b = 123
b = 127

Difference between Int16 and UInt16 in C#


C# Int16 and UInt16 Differences: Here, we are going to learn about the differences
between C# Int16 and C# UInt16 data types.
Submitted by IncludeHelp, on February 11, 2019

C# Int16 and C# UInt16


In C#, Int16 known as a signed integer of 2 bytes which can store both types of
values including negative and positive between the ranges of -32768 to +32767.

UInt16 known as an unsigned integer of 2 bytes which can store only positive values
between the ranges of 0 to 65535.
Differences between 'Int16' and 'UInt16'
Int16 UInt16

Int16 stands for signed integer. UInt16 stands for unsigned integer.

It's capacity to store the value is -32768 to +32767. It's capacity to store the value is 0 to 65535.

It can store negative and positive integers. It can store only positive integers.

It occupies 2-bytes space in the memory. It also occupies 2-bytes space in the memory.

Declaration syntax: Declaration syntax:


Int16 variable; UInt16 variable;

Example:

In this example, to explain the differences between Int16 and UInt16 in C#, we are
printing their minimum and maximum values, we are also declaring two arrays – arr1 is
a signed integer type and arr2 is an unsigned integer type. Initializing the arrays with
corresponding negative and positive values based on their capacity.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//Int16 value range
Console.WriteLine("Int16 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
Int16.MinValue, Int16.MaxValue);

//UInt16 value range


Console.WriteLine("UInt16 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
UInt16.MinValue, UInt16.MaxValue);
//Int16 array
Int16[] arr1 = { -32768, 0, 1000, 32000, 32767};
Console.WriteLine("UInt16 array elements...");
foreach (Int16 num in arr1)
{
Console.WriteLine(num);
}
Console.WriteLine();

//UInt16 array
UInt16[] arr2 = { 0, 100, 23000, 65000, 65525};
Console.WriteLine("UInt16 array elements...");
foreach (UInt16 num in arr2)
{
Console.WriteLine(num);
}

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Int16 value capacity...


Min: -32768, Max: 32767

UInt16 value capacity...


Min: 0, Max: 65535

UInt16 array elements...


-32768
0
1000
32000
32767

UInt16 array elements...


0
100
23000
65000
65525
Difference between Int64 and UInt64 in C#
C# Int64 and UInt64 Differences: Here, we are going to learn about the differences
between C# Int64 and C# UInt64 data types.
Submitted by IncludeHelp, on February 11, 2019

C# Int64 and C# UInt64


In C#, Int64 known as a signed integer of 8 bytes which can store both types of
values including negative and positive between the ranges of -9223372036854775808
to +9223372036854775807.

UInt64 known as an unsigned integer of 8 bytes which can store only positive values
between the ranges of 0 to 18446744073709551615.

Difference between Int32 and UInt32 in C#


C# Int32 and UInt32 Differences: Here, we are going to learn about the differences
between C# Int32 and C# UInt32 data types.
Submitted by IncludeHelp, on February 11, 2019

C# Int32 and C# UInt32


In C#, Int32 known as a signed integer of 4 bytes which can store both types of
values including negative and positive between the ranges of -2147483648 to
+2147483647.

UInt32 known as an unsigned integer of 4 bytes which can store only positive values
between the ranges of 0 to 4294967295.

Differences between 'Int32' and 'UInt32'


Int32 UInt32

Int32 stands for signed integer. UInt32 stands for unsigned integer.


It's capacity to store the value is -2147483648 to +2147483647. It's capacity to store the value is 0 to 429496

It can store negative and positive integers. It can store only positive integers.

It occupies 4-bytes space in the memory. It also occupies 4-bytes space in the memor

Declaration syntax: Declaration syntax:


Int32 variable; UInt32 variable;

Example:

In this example, to explain the differences between Int32 and UInt32 in C#, we are
printing their minimum and maximum values, we are also declaring two arrays – arr1 is
a signed integer type and arr2 is an unsigned integer type. Initializing the arrays with
corresponding negative and positive values based on their capacity.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//Int32 value range
Console.WriteLine("Int32 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
Int32.MinValue, Int32.MaxValue);

//UInt32 value range


Console.WriteLine("UInt32 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
UInt32.MinValue, UInt32.MaxValue);

//Int32 array
Int32[] arr1 = { -2147483648, 0, 12320009, 2147480000,
2147483647 };
Console.WriteLine("UInt32 array elements...");
foreach (Int32 num in arr1)
{
Console.WriteLine(num);
}
Console.WriteLine();

//UInt32 array
UInt32[] arr2 = { 0, 100, 23000, 4294960000, 4294967295
};
Console.WriteLine("UInt32 array elements...");
foreach (UInt32 num in arr2)
{
Console.WriteLine(num);
}

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Int32 value capacity...


Min: -2147483648, Max: 2147483647

UInt32 value capacity...


Min: 0, Max: 4294967295

UInt32 array elements...


-2147483648
0
12320009
2147480000
2147483647

UInt32 array elements...


0
100
23000
4294960000
4294967295
ADVERTISE

Differences between 'Int64' and 'UInt64'


Int64 UInt64

Int64 stands for signed integer. UInt64 stands for unsigned integer.

It's capacity to store the value is -9223372036854775808 to It's capacity to store the value is 0 to
+9223372036854775807. 18446744073709551615.

It can store negative and positive integers. It can store only positive integers.

It occupies 8-bytes space in the memory. It also occupies 8-bytes space in the memo

Declaration syntax: Declaration syntax:


Int64 variable; UInt64 variable;

Example:

In this example, to explain the differences between Int64 and UInt64 in C#, we are
printing their minimum and maximum values, we are also declaring two arrays – arr1 is
a signed integer type and arr2 is an unsigned integer type. Initializing the arrays with
corresponding negative and positive values based on their capacity.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//Int64 value range
Console.WriteLine("Int64 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
Int64.MinValue, Int64.MaxValue);

//UInt64 value range


Console.WriteLine("UInt64 value capacity...");
Console.WriteLine("Min: {0}, Max: {1}\n",
UInt64.MinValue, UInt64.MaxValue);

//Int64 array
Int64[] arr1 = {
-9223372036854775808,
0,
1287822320009,
9223372036854700000,
9223372036854775807
};
Console.WriteLine("UInt64 array elements...");
foreach (Int64 num in arr1)
{
Console.WriteLine(num);
}
Console.WriteLine();

//UInt64 array
UInt64[] arr2 = {
0,
1239289300,
2399900900022,
18446744073709000000,
1844674407370955161
};
Console.WriteLine("UInt64 array elements...");
foreach (UInt64 num in arr2)
{
Console.WriteLine(num);
}

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Int64 value capacity...


Min: -9223372036854775808, Max: 9223372036854775807

UInt64 value capacity...


Min: 0, Max: 18446744073709551615

UInt64 array elements...


-9223372036854775808
0
1287822320009
9223372036854700000
9223372036854775807
UInt64 array elements...
0
1239289300
2399900900022
18446744073709000000
1844674407370955161

Difference between int, Int16, Int32 and Int64 in


C#
C# differences between int, Int16, Int32 and Int64: Here, we are going to learn what
are the differences between int, Int16, Int32 and Int64 in C#?
Submitted by IncludeHelp, on February 13, 2019

int, Int16, Int32 and Int64 are used to represent signed integers with values ranging
based on their capacities/occupied size in the memory. These types are able to work
with negative and positive values. All these types are the same in nature but different
based on the value ranges.

Difference between int, Int16, Int32 and Int64


1) Int16
 Int16 represents 16-bits (2-bytes) signed integer.
 Int16 occupies 16-bits (2-bytes) space in the memory.
 As per the 2-bytes data capacity, an Int16's value capacity is -32768 to +32767.

Example:

Consider the code – Here, we are printing required size, type, minimum & maximum
value, variable declaration, and assignment of an Int16.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//printing Int16 capacity, type, MIN & MAX value
Console.WriteLine("Int16 occupies {0} bytes",
sizeof(Int16));
Console.WriteLine("Int16 type is: {0}", typeof(Int16));
Console.WriteLine("Int16 MIN value: {0}",
Int16.MinValue);
Console.WriteLine("Int16 MAX value: {0}",
Int16.MaxValue);
Console.WriteLine();

//Int16 variables
Int16 a = 12345;
Int16 b = -12345;
Console.WriteLine("a = {0}, b = {1}", a, b);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

Int16 occupies 2 bytes


Int16 type is: System.Int16
Int16 MIN value: -32768
Int16 MAX value: 32767

a = 12345, b = -12345

Difference between uint, UInt16, UInt32 and


UInt64 in C#
C# differences between uint, UInt16, UInt32 and UInt64: Here, we are going to
learn what are the differences between uint, UInt16, UInt32 and UInt64 in C#?
Submitted by IncludeHelp, on February 13, 2019

uint, UInt16, UInt32 and UInt64 are used to represent unsigned integers with values
ranging based on their capacities/occupied size in the memory. These types work with
positive values only. All these types are the same in nature but different based on the
value ranges.

Difference between uint, UInt16, UInt32 and UInt64


1) UInt16
 UInt16 represents 16-bits (2-bytes) unsigned integer.
 UInt16 occupies 16-bits (2-bytes) space in the memory.
 As per the 2-bytes data capacity, an UInt16's value capacity is 0 to +65535.

Example:

Consider the code – Here, we are printing required size, type, minimum & maximum
value, variable declaration, and assignment of an UInt16.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//printing UInt16 capacity, type, MIN & MAX value
Console.WriteLine("UInt16 occupies {0} bytes",
sizeof(UInt16));
Console.WriteLine("UInt16 type is: {0}",
typeof(UInt16));
Console.WriteLine("UInt16 MIN value: {0}",
UInt16.MinValue);
Console.WriteLine("UInt16 MAX value: {0}",
UInt16.MaxValue);
Console.WriteLine();

//UInt16 variables
UInt16 a = 12345;
UInt16 b = 65000;
Console.WriteLine("a = {0}, b = {1}", a, b);

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

UInt16 occupies 2 bytes


UInt16 type is: System.UInt16
UInt16 MIN value: 0
UInt16 MAX value: 65535

a = 12345, b = 65000

What is boxing and unboxing in C#?


Learn: What is boxing and unboxing in C#.Net with definitions, declaration and
Example?

There are 2 types in c# one is value type and other is reference type. Object is the base
class of all the types in C#.

When a value type is converted into object type then this implicitly conversion
process is called boxing. On the other hand when an object type is converted back
to its value type explicitly then it is called as unboxing.

Example:

int a = 10;

// Here value of variable a is boxed and assigned into variable o;

object o = a;

The value of object o need to be unboxed at time of retrieval.

int a =(int)o;

An explicit conversion is required while unboxing process.


Command line argument in C# with Example
Learn: What are Command Line Arguments in C#.Net with Example? Which is used to
pass messages while calling the executable file from command line.

Command line argument is very important concept in every programming language.


Using this we can pass arguments to main function at runtime. As we know that main
function contains arguments of string array. It can accept command line argument from
console.

Consider the program:

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);

}
}

Output

D:\>sample 10 20
Sum is: 30

In this program, args of main function accepts argument values from console when we
execute the program.

Here, we are passing 10 and 20 as command line arguments which can be accessed


through args. Where 10 will be assigned into args[0] and 20 will be assigned
into args[1]

What is difference between object, var and


dynamic keywords in C#?
Learn: What are an object, var and dynamic keywords in C#.Net? What are the
differences between them?

Object
It is a type which is base of all the types and one of the oldest features of the language.
It means values of any types can be stored in object type variable. But type conversion
(un-boxing) is required to get original type when the value of variable is retrieved in
order to use it.

Object o;

o = 10;
So it is an extra overhead of un-boxing. Thus object type should only be preferred if we
have no more information about the data type.

Compiler has title information of object type.

Var
This was introduced in .net 3.5 framework. This also can have any type of values like
object but it is must to initialize the value at time of declaration of the variable. The
keyword is best suited for linq queries in C#.

Var a = 10;

Var b = "string value";

Var c = new Product(); // object of product type

Var d;//Error, as the value is not assigned at time of declaration of


variable d

d = 10;

Dynamic
This keyword was introduced in .net 4.0 framework. This also can be used to store any
type of value like object and var but unlike object un-boxing is not required at time of
using the variable.

Dynamic d1 = 10;

Dynamic d2;

D2=new Product();

Dynamic d3="string value";

Compiler has no information of dynamic type but value is evaluated at run time.

Difference between structure and class in C#


C# structure and class differences: Here, we are going to learn what are the
differences between structures and classes in C#?
Submitted by IncludeHelp, on February 12, 2019

C# class and structure


In C# and other programming languages, structure and classes are used to define a
custom data type, that we can organize according to our need with different types of
variables, methods etc.

Both are not the same. Here, we are writing differences between structure and class,
they have the following basic differences...

Differences between C# classes and C# Structures


1. Classes are references types of data type, structures are value type of data type.
2. Classes support default constructor i.e. we can set default values that will be
assigned while creating an object. Structures do not support the concept of the
default constructor, we cannot set values like classes that can be used as default
values while creating a structure object/variable.
3. Classes support the inheritance; structures do not support the inheritance.

Example:

In this example, we are creating a structure student_1 and a class student_2 along


with the methods. To understand the difference between a class and structure in C#,
please practice the given example.

using System;
using System.Text;

namespace Test
{
//structure
public struct student_1{
private string name;
private short age;
private float perc;

//method
public void setValue(string name, short age, float perc)
{
this.name = name;
this.age = age;
this.perc = perc;

}
public void dispValues()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("age: {0}", age);
Console.WriteLine("perc: {0}", perc);
}
};

//class
public class student_2{
private string name;
private short age;
private float perc;

//default constructor
public student_2()
{
this.name = "N/A";
age = 0;
perc = 0.0f;
}
//method
public void setValue(string name, short age, float perc)
{
this.name = name;
this.age = age;
this.perc = perc;

}
public void dispValues()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("age: {0}", age);
Console.WriteLine("perc: {0}", perc);
}
};

class Program
{
static void Main(string[] args)
{
//creating structure variable
student_1 std1 = new student_1();
//printing default values
Console.WriteLine("std1 (default values)...");
std1.dispValues();
//setting values
std1.setValue("Amit", 21, 98.23f);
//printing after setting the values
Console.WriteLine("std1 (after setting values)...");
std1.dispValues();

Console.WriteLine();

//creating class object


student_2 std2 = new student_2();
//defaut constructor will be invoked
//printing values which we set in default constructor
Console.WriteLine("std2 (default values)...");
std2.dispValues();
//setting values
std2.setValue("Amit", 21, 98.23f);
//printing after setting the values
Console.WriteLine("std2 (after setting values)...");
std2.dispValues();

//hit ENTER to exit


Console.ReadLine();
}
}
}

Output

std1 (default values)...


Name:
age: 0
perc: 0
std1 (after setting values)...
Name: Amit
age: 21
perc: 98.23

std2 (default values)...


Name: N/A
age: 0
perc: 0
std2 (after setting values)...
Name: Amit
age: 21
perc: 98.23
Searching in C#.net, types of searching and
implementations using C# program
In this article, we will learn about C# .Net searching, its types (Linear/Sequential and
Binary) and implementation. Here, you will also find the solved programs on
searching in C#.Net.

Searching is the technique to find particular item, here we are discussing some of
the popular searching techniques to find an item from the array.

For example we have a list: 12 15 11 8 34 48

Here we will find item 11 which is exist on 3rd (according to array indexing it’s on 2nd)
position.

In context of C#, here we will take an array that contains integer elements. Then we will
write code to search element from array.

Types of searching
Basically there are two type of searching is used in computer technology:

1. Internal Searching
2. External Searching

1) Internal Searching: Searching an item into main memory or primary memory is


known as internal searching.

2) External Searching: Searching an item into external or secondary memory is known


as external searching.

In arrays or linked list we always perform internal searching. There are some popular
methods used in searching are given below:

1. Linear or sequential search


2. Binary search
1) Linear or sequential searching implementation in C#
In linear/sequential search, we search given item, sequentially one by one, if we found
item, then we return location. It may also possible item is not find till last item of list.

For example we have list: 12 13 10 25 47

Suppose we have an item 25 to search.

 In first comparison we compare 25 with 12 but her we did not find match.
 In second comparison we compare 25 with 13 but here we did not find match.
 In third comparison we compare 25 with 10 but still here we did not find match.
 In forth comparison we compare 25 with 25. Here we found matching element, then we
can say 25 have 4th position in given list.

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int i = 0 ;
int item = 0 ;
int pos = 0 ;

int[] arr = new int[5];

//Read numbers into array


Console.WriteLine("Enter elements : ");
for (i = 0; i < arr.Length; i++)
{
Console.Write("Element[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}

Console.Write("Enter item to search : ");


item = int.Parse(Console.ReadLine());
//Loop to search element in array.
for (i = 0; i < arr.Length; i++)
{
if (item == arr[i])
{
pos = i + 1;
break;
}
}

if (pos == 0)
{
Console.WriteLine("Item Not found in array");
}
else
{
Console.WriteLine("Position of item in array:
"+pos);
}

}
}
}

Output

Enter elements :
Element[1]: 25
Element[2]: 12
Element[3]: 13
Element[4]: 16
Element[5]: 29
Enter item to search : 16
Position of item in array: 4

Binary searching implementation in C#


In this searching, we divide list for searching, here we use 3 variables LOW, HIGH, MID.

LOW - It indicates lower index of list.


HIGH - It indicates highest index of list.
MID - It indicates middle index of list.

For example we have a list in ascending order. 10 20 30 40 50 60 70 80 90 100

And we find item 30 in above list.


Note: Array must be sorted

1st comparison:
LOW = 1
HIGH = 10
MID = (1+10) /2 = 5

Now if LIST[MID] == ITEM, (50 == 30) No Match.

Then find new indices by dividing list. Here ITEM is less than mid
value then:
LOW = 1
HIGH = MID -1 =4
MID = (1+4)/2 = 2

2nd comparison:
IF LIST[MID] == ITEM, (20==30) No Match.

Then find new indices by dividing list. Here ITEM is greater than
mid value then:
LOW = MID + 1 = 3
HIGH = 4
MID = (3+4)/2 = 3

3rd comparison:
IF LIST[MID] == ITEM, (30==30), No ITEM found at position 3.

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int i = 0 ;
int item = 0 ;
int pos = 0 ;

int[] arr = new int[5];


int LOW = 0;
int HIGH = 0;
int MID = 0;

//Read numbers into array


Console.WriteLine("Enter elements : ");
for (i = 0; i < arr.Length; i++)
{
Console.Write("Element[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}

Console.Write("Enter item to search : ");


item = int.Parse(Console.ReadLine());

HIGH = arr.Length - 1;
MID = (LOW + HIGH) / 2;

//Loop to search element in array.


while (LOW <= HIGH)
{
if (item < arr[MID])
{
HIGH = MID - 1;
MID = (LOW + HIGH) / 2;
}
else if (item > arr[MID])
{
LOW = MID + 1;
MID = (LOW + HIGH) / 2;
}
else if (item == arr[MID])
{
pos = MID + 1;
break;
}
}

if (pos == 0)
{
Console.WriteLine("Item Not found in array");
}
else
{
Console.WriteLine("Position of item in array:
"+pos);
}
}
}
}

Output

Enter elements :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Enter item to search : 40
Position of item in array: 4

You might also like