Variables and Constants - C# FOR BEGINNERS CRASH COURSE (2014)

C# FOR BEGINNERS CRASH COURSE (2014)

Chapter 4 Variables and Constants

4.1 Exploring variables in C#

A variable is name assigned to the memory location used by the programs. Every variable has a data type associated with it. The data type determines the size of the variable’s memory used for storing within the memory.

The data types provided by C# are distinguished as:

1. Integral types: int, unit, short, byte, sbyte, long, ulong, and char

2. Floating point types: double, float

3. Decimal types: decimal

4. Boolean type: true or false values

5. Nullable type: nullable data type

6. Reference type: class

Defining Variables

The syntax for declaring the variables is:

<data_type> <variable_list>;

Where, data_type is a valid C# data type. It can be int, char, float, double, or a user defined type. The variable_list contains one or more identifiers.

Examples of variables

int a,b,c;

char x,y;

float price, totolcost;

double area;

Initializing Variables

The variables are initialized using an equal sign and followed by an expression. The syntax for initializing variable is:

variable_name = value;

The variables can be initialized in the declaration. The general form for initializing the variable is:

<data_type> <variable_name> = value;

Examples of variable initialization are:

int a = 4;

char z=’z’;

double y = 12.145;

Example for demonstrating variable types:

Example 5:

using System;

namespace VariableDeclare

class Program

{

static void Main(string[] args)

{

int a;

short s;

double d;

/*initializing variables*/

a=20;

s=5;

d=a+s;

Console.WriteLine("a={0},b={1},d={2}",a,s,d);

Console.Read();

}

}

Accepting values from user

The Console class present in the System namespace provides ReadLine() function. The function is used for accepting input from the user and stored into variable.

Example:

int no;

no=Convert.ToInt32(Console.ReadLine());

The Convert.ToInt32() function converts the data entered by the user to integer data type. The function accepts the data in string format.

LValue and RValue Expressions

The two types of expressions in C# are:

· lvalue: The expression is an lvalue appears on the left side of the assignment.

· rvalue: The expression is an rvalue that appears on the right side but not on the left side of the assignment.

The variables are lvalues and hence appear on the left side of the assignment. The numeric values are rvalues and hence appear on the right side.

int a=50;

4.2 Constants and literals in C#

Constant is a class member that represents a fixed value. Constant value can be computed at compile time but cannot be modified. Constants are declared using the const keyword.

Syntax:

const<data_type> <constant_name> = value;

Example of Constant:

Example 6:

using System;

namespace constc

class Program

{

static void Main(string[] args)

{

const int i=3;

int x;

Console.WriteLine("The value for x is");

x=Convert.ToInt(Console.ReadLine());

int mult = x*i;

Console.WriteLine("Value of multiplication is:{0}",mult);

Console.ReadLine();

}

}

Literals

A literal is a source code representation of a value. There are different types of literals in C#.

Integer Literals

Integer literal is used to write values of type int, long, unit, and ulong. It can be represented as decimal, octal, or hexadecimal constant. The base or radix is specified by the prefix. The value 0x or 0X represents the hexadecimal, 0 defines octal and decimal is used without prefix.

Some of the examples of integer literals are 15, 0321, 0x5b, 30l.

Floating point literals

Floating point literal consist of integer part, fraction part, decimal part, and an exponent part. The floating point literals can be represented in exponent or decimal form.

Floating point literals can be mentioned as 3.1415, 1423E-6L.

Character Literals

Character literals represent a single character. They are enclosed in single quotes. Characters that are preceded with a backslash are called escape characters and some of these escape characters have special meanings when used in a string to designate a special function such as creating a tab or a carriage return. Below is a list of some of these special escape characters:

· \’ A single quote

· \” Double quote

· \\ Backslash

· \0 null

· \a Alert

· \b Backspace

· \f Form feed

· \t Horizontal tab

· \v Vertical tab

· \r Carriage return

String Literals

There are two string literals types supported by C#: regular string literals and verbatim string literals. Regular string literals contain zero or more characters enclosed in double quotes.

A verbatim string literals contains @ character followed by a double quote character, zero or more character. A verbatim string literal can span multiple lines.

Examples showing string literals.

“welcome user”

“welcome, \

user”

@”welcome user”