HOME  |    TRAINING  |   FREE TUTORIALS
Find out more about our new RSS feed.
FREE Tutorial
C# VARIABLES

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Like Java and C/C++, C# is a type-safe language. Variables that are declared to be of a particular type are constrained to hold values of that type. This tutorial takes a brief look at Variables.


TUTORIAL TAKEN FROM COURSE : C# PROGRAMMING

FULL COURSE DETAILS

At the end of this course, you will have learned the fundamental skills that are required to design and develop object-oriented applications for the Web and Microsoft Windows® by using Microsoft Visual C#® .NET and the Microsoft Visual Studio® .NET development environment. This course provides an alternative entry point for less experienced programmers who are not familiar with object-oriented design and programming with Windows or the Web.

TO ACCESS THE FULL COURSE AND HUNDREDS OF OTHERS, CLICK HERE.


Variables

Variables represent storage locations. Every variable has a type that determines the values to be stored in the variable. C# is a type-safe language and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through assignment or through use of the ++ and - operators.

Variables are values that can change as much as needed during the execution of a program. One reason you need variables in a program is to hold the results of a calculation. Hence, variables are locations in memory in which values can be stored.

Variable Declarations

In C# all variables must be declared before they are used. In the declaration you must specify a data type and a name for the variable so that memory can be set aside for the variable. An optional initial value can be included with the declaration if you know what the value should be to start.

Syntax for variable declaration

[scope] [=initial value];

Description

The scope determines the accessibility of the variable (public, local, private) and the default is private. The data type specifies what kind of data the variable will hold. Optionally the variable can be initialised to a specific value. All variables must be initialised (given a value) before they are used in any calculation. If the variables are not initialised, the compilation of the program will fail.

Value Type Variables

Value type variables are also known as stack variables because they are stored on the stack. Value type variables can be directly declared and referenced. As the variables go out of scope, they are removed from the stack, ensuring the proper destruction of the variables. As the variables are created on the stack, they are not initialised; that is the responsibility of the program. The use of an uncapitalized variable will result in a compiler error.

Example Value variable

Int n; //uncapitalized int
Long l = 327 //initialised long
Float f = 3.13 F; //float initialised from single-precision literal

Reference Type Variables

Reference type variables are made up of two parts: the reference on the stack and the object on the heap. The creation of the object and the reference to the object is commonly known as the instantiation of the object.

Example Reference variable

To declare a reference-type variable, the syntax used are:

string strMimico; city objToronto = null; object objGeneric;

Example Object variable

To create an object on the heap, we go through two steps, as follows:

1. Declare the variable reference.
2. Instantiate the object on the heap by calling the new operator.

City objMimico; //declare objMimico to be a reference to a an
// object of City type
objMimico = new City(); //call the constructor of the City class to return
// a reference that is stored in the objMimico reference


The two lines can be combined into one:

City objMimico = new City();




5 RELATED COURSES AVAILABLE
C# PROGRAMMING
At the end of this course, you will have learned the fundamental skills that are required to design and develop o....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
CONTACT US
Tuesday 22nd May 2012  © COPYRIGHT 2012 - website design by Website Design by Visualsoft