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

Unit2 1 PDF

The document discusses data types in C#. It describes that C# is a type-safe language where variables are declared with a specific type. There are two main types of data types in C#: value types and reference types. It then provides details on the predefined and integral data types in C#, including their size, range of values, and examples of usage.

Uploaded by

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

Unit2 1 PDF

The document discusses data types in C#. It describes that C# is a type-safe language where variables are declared with a specific type. There are two main types of data types in C#: value types and reference types. It then provides details on the predefined and integral data types in C#, including their size, range of values, and examples of usage.

Uploaded by

Monti Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data types in C#

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.

 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 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

ushort System.Int16 16-bits 0 to 65,535 Unsigned 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

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.

Syntax: bool variable_name = value;

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

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

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

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

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

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

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.

An enum is defined by using the enum keyword.

Syntax: enum enum_name {enumeration_list };

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
}

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

//hit ENTER to exit


Console.ReadLine();
}
}
}

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;

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

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

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

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.

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:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy