Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 7


In previous article, we have discussed what is procedural Programming, entry point of a programming, what is object oriented approach, what is class and object, CPP program, Java Program, what are modifiers, how to write a C# program, how to compile the C# Program and explanation of each line in the example program, and also discuss what is the namespace and how we can import a namespace in the program

Please find below the link for accessing the article
CSharp.NET Tutorial - Day 6 

Now, in this article we will discuss what are the Data Types available in C# like Integer types, Decimal Types, Boolean Types, Character Types, and Generic Types and also will discuss constant and readonly variables, Value Types, Reference Types, Implicitly typed Variables, what are Nullable Value Types, what is Boxing and UnBoxing, what are Operators available in C#, etc

Data Types

Integer types

CSharp Types
IL Types
Size
Byte
System.Byte
1
Short
System.Int16
2
Int
System.Int32
4
Long
System.Int64
8
Sbyte
System.SByte
1
Ushort
System.UInt16
2
Uint
System.UInit32
4
Ulong
System.UInt64
8

byte, ushort, uint and ulong will store only assigned values, whereas sbyte, short, int and long types can store signed values in them.

Decimal Types

CSharp Types
IL Types
Size
Float
System.Single
4
Double
System.Double
8
Decimal
System.Decimal
16

Boolean Types

CSharp Types
IL Types
Size
Bool
System.Boolean
1

Character Types

CSharp Types
IL Types
Size
Char
System. Char
2
String
System.String
-

Note:
·         The size of char type has been increased to 2 bytes for providing support to Unicode characters (Language apart from English).
·         String is a variable length type, which doesn't have any fixed size.

Generic Types

CSharp Types
IL Types
Size
object
System.Object
-

Object type is capable of storing any value in it, which is also a variable length type.

Syntax

[<modifiers>] [const] [readonly] <type><var>[=value][...n]

Eg:

int x;
int y=100;
string s1="Hello", s2;
public const float pi=3.14f;
public readonly decimal d=567.890m;

A “constant” and “readonly” variables are similar whose values cannot be modified after assignment.

Every decimal value will be internally treated as double, so if we want a float value it should be suffixed with 'f', and in case of decimal value it should be suffixed with 'm'.

Eg:

using System;
class VarsDemo
{
static void Main()
{
int x = 100;
Console.WriteLine (x.GetType ());
float f = 3.14f;
Console.WriteLine (f.GetType ());
decimal d = 545.454m;
Console.WriteLine (d.GetType ());
}
}

Note:  GetType is a predefined method that returns the type of a given variable or object.

Data types are classified into two categories
1.      Value Types
2.      Reference Types

Value types store the data on "stack", which is the place where data stores in “fixed length” such as int, float, etc.

Stack works on a principle called First in Last out (FILO) or Last in First out (LIFO).

Every program has its own stack and no other program can share it and it will be under the control of operating system, which doesn't provide automatic memory management but it is faster in access.

Reference Types are stored on “Heap” memory, which is the place where data can be stored.  CSharp supports two predefined reference types, one is “object” and the second one is “String”.

In .NET, the Heap is now more managed and also called as “Managed Heap” and it will be under the control of Garbage Collector.

Implicitly typed Variables
It is a new feature that has been added in C# 3.0 and it allows us to declare a variable using “var” keyword.  We can identify the type of variable at the time of its compilation depending upon the values we assigned.

var x = 100; --------> x is of type int
var s ="Hello"; --------> s is of type string
var f =3.14f; --------> f is of type float

Note:  Please make sure that in this process, declaring a variable without assigning a value is not possible.

Eg:

var y; ---> Invalid
Eg:

using System;
class TypesDemo
{
static void Main()
{
var x = 100;
Console.WriteLine (x.GetType ());
var f = 3.14f;
Console.WriteLine (f.GetType ());
var d = 3.14m;
Console.WriteLine (d.GetType ());
}
}

Nullable Value Types
In the CSharp earlier versions, it is not possible to store NULL values under value types, but from version 2.0, it is possible to store NULL values under value types also, which gives us the more flexibility while communicating with the database.

To declare a Nullable value type, we need to suffix the type name with question (?) mark.

Eg:
string str=null; -->valid
int x=null; -->Invalid
int? x=null; -->Valid

Boxing and UnBoxing
Boxing and unboxing is an important concept in .NET’s type system.  With Boxing and unboxing, we can link between value types and reference types by allowing any value of a value type that is to be converted to and from type object.  Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object.



Converting a value type to reference type is called Boxing.  If a value type is assigned to a Reference type variable, we call it boxing.

int x=100;
object obj=x; -->Boxing

Unboxing is the opposite operation and is an explicit operation.  Creating a Reference type back into value type is known as UnBoxing.

int y=(int) obj; -->UnBoxing

Operators

Arithmetic
+,-,*, /, %
Assignment
=, +=,-=,*=, /=, %=
Comparison
==,! =, <, <=,>,>=, is, as, Like
Concatenation
+
Increment and decrement
++,--
Logical
&&, ||, ^


CSharp.NET Tutorial - Day 8


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 7

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×