C-Sharp Variable Types

Hello friends, In this article you will learn about C# Variable Types i.e. Value Type and Reference Type. Moreover Boxing and Unboxing.

C# have two types of variables that is Value Type and Reference Type.

Value type variables has data directly, Whereas variables of Reference Type stores memory address where their data is located. Reference Type is known as Object. In case of Reference Type variables, if two variables reference the same object, than any change in value of one variables will affect the other. In case of Value type two copies will be created, and no affect appear if one variable value change.

Value types are further divided into:
simple types, enum types, struct types, and nullable value types.

Simple Signed Type: sbyte, short, int, long
Unsigned integral: byte, ushort, uint, ulong
Unicode characters: char
floating point: float, double
High-precision decimal: decimal
boolean: boolean
Enum: enum e{}
Stucture: struct s {}
Nullable: null

Reference types are further divided into:
class types, interface types, array types, and delegate types.

Class: class c{}
Interface: interface I {}
Array: int[], int[,]
Delegate: delegate int D()

C# Numeric Variable Ranges

Signed Integral

sbyte : 8 bits, range from -128 to 127
short : 16 bits, range from -32,768 to 32,767
int : 32 bits, range from -2,147,483,648 to 2,147,483,647
long : 64 bits, range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Unsigned integral

byte : 8 bits, range from 0 to 255
ushort : 16 bits, range from 0 to 65,535
uint : 32 bits, range from 0 to 4,294,967,295
ulong : 64 bits, range from 0 to 18,446,744,073,709,551,615

Floating point

float : 32 bits, range from 1.5 × 10-45 to 3.4 × 1038, 7-digit precision
double : 64 bits, range from 5.0 × 10-324 to 1.7 × 10308, 15-digit precision

Decimal

decimal : 128 bits, range is at least -7.9 × 10-28 to 7.9 × 1028, with at least 28-digit precision

type in C# derives from the object class type, and object is the base class of all C# types. Values of reference types are treated as objects. Values of value types are treated as objects by boxing and unboxing operations.

Boxing

When a value of a value type is converted to type object is called Boxing.

Unboxing

when an object reference is converted into a value type is called Unboxing.

Following examples shows boxing and unboxing

using System; class BoxingExample { static void Main() { int i = 123; // Boxing object o = i; // Unboxing int j = (int)o; } }

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories