C# tutorial-Variables and Data types

C# Variables and Data types



- Variable

A variable is referred to a memory location in which a value can be stored temporarily. You can write C# code to read the value from the memory location or write something to it.

- Data type

Data types of variables tell the computer to store different types of values such as number, text, or true/false…They also inform the computer to reserve the different memory’s spaces for those variables.

Here is a table of the data types of variables that can be used:

Keyword

Type

Values

sbyte

Numeric-Integer

-128 to 127

byte

Numeric-Integer

0 to 255

short

Numeric-Integer

-32,768 to 32,767

ushort

Numeric-Integer

0 to 65,535

int

Numeric – Integer

-2 147 483 648 to 2 147 483 647

uint

Numeric-Integer

0 to 4,294,967,295

long

Numeric-Integer

- 9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

ulong

Numeric-Integer

      0 to 18,446,744,073,709,551,615

float

Numeric – Real

1.5 x 10-45 to 3.4 x 1038

double

Numeric – Real

5.0 x 10-324 to 1.7 x 10308

decimal

Numeric-Real

1.0 x 10-28 to 7.9 x 1028

char

Character

All Unicode characters

string

Unicode string

A Unicode string with no special upper bound

bool

Boolean

True or False

object

Pointer to an object

Any objects

DateTime

Date/time

Date or time value


- Declaring variables

Before you can use a variable, you must declare it. In C#, to declare a variable you need to specify data type of the variable and its name. To declare a variable a to store an integral value, you will write as the following:

int a; //declaring a variable named a to store integer value

 

You can declare more than one variables of the same type on a single line by separating them with commas.

int a,b;

float i, j;

 

- Assigning values to variables

You can assign values to the variables by using assignment operator (=).

int i = 5;
char c='a';
bool b=true;

or

int a;
char c;
bool b;
a= 5; 
c='a';
b=true;

 

- Type conversion

C# allows you to convert explicitly from a type to other allowable types by specifying the type to be converted to in parentheses.

int x=10;

float y;

y=(float) x;//convert int type to float type

 

If you want to convert a string that contains a number to an integral/float/double number, you can use parse() method of int/float/double class.

string x = "123";

int i = int.Parse(x);

float f=float.Parse(x);

double d=double.Parse(x);




Comments

KrishnaMohan comment

 KrishnaMohan

Check this also for c# interview questions
http://skillgun.com/csharp/interview-questions-and-answers

07-22-2014
uma comment

 uma

Nice site thanks

10-23-2013
Dara comment

 Dara

Searching and sorting Khmer Unicode characters still be a problem today.

10-01-2013
vandy comment

 vandy

how to search khmer unicode in VB.net? i can't find definition of word(ex: áá¶áá¶) in my dictionary.

09-30-2013
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like