Data Types in C
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.
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:
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();
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.
Syntax:
There are only two possible values that can be assigned to a bool variable 1) true or
2) false
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));
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.
Syntax:
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));
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.
Syntax:
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));
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.
Syntax:
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));
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.
Syntax:
It can store the value between the range of ±1.0 x 10 -28 to ±7.9228 x 1028.
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));
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.
Syntax:
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);
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.
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:
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);
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.
Syntax:
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);
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.
Syntax:
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);
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.
Syntax:
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);
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.
Syntax:
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);
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.
Syntax:
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);
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.
Syntax:
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);
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
Syntax:
Example 1:
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);
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#.
Syntax:
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);
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:
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";
Output
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.
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.
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;
b = -100;
Console.WriteLine("b = " + b);
b = 123;
Console.WriteLine("b = " + b);
b = 127;
Console.WriteLine("b = " + b);
Output
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
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.
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 array
UInt16[] arr2 = { 0, 100, 23000, 65000, 65525};
Console.WriteLine("UInt16 array elements...");
foreach (UInt16 num in arr2)
{
Console.WriteLine(num);
}
Output
UInt64 known as an unsigned integer of 8 bytes which can store only positive values
between the ranges of 0 to 18446744073709551615.
UInt32 known as an unsigned integer of 4 bytes which can store only positive values
between the ranges of 0 to 4294967295.
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
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);
//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);
}
Output
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
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);
//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);
}
Output
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.
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);
Output
a = 12345, b = -12345
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.
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);
Output
a = 12345, b = 65000
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;
object o = a;
int a =(int)o;
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;
}
}
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.
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.
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;
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();
Compiler has no information of dynamic type but value is evaluated at run time.
Both are not the same. Here, we are writing differences between structure and class,
they have the following basic differences...
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();
Output
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.
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
In arrays or linked list we always perform internal searching. There are some popular
methods used in searching are given below:
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.
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 ;
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
1st comparison:
LOW = 1
HIGH = 10
MID = (1+10) /2 = 5
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.
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 ;
HIGH = arr.Length - 1;
MID = (LOW + HIGH) / 2;
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