0% found this document useful (0 votes)
17 views5 pages

Chapter 4

Chapter 4 discusses variables in C#, categorizing them into value types, reference types, and pointer types, along with their syntax and naming conventions. It details common data types, including integral, floating-point, boolean, and textual types, and explains the use of constants and the distinction between local and global variables. The chapter also provides examples of variable initialization and usage within a C# program.

Uploaded by

krmydn7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Chapter 4

Chapter 4 discusses variables in C#, categorizing them into value types, reference types, and pointer types, along with their syntax and naming conventions. It details common data types, including integral, floating-point, boolean, and textual types, and explains the use of constants and the distinction between local and global variables. The chapter also provides examples of variable initialization and usage within a C# program.

Uploaded by

krmydn7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 4

1. Variables

Variables are containers for storing data values. The variables in C#, are categorized into the following
types:

• Value types: Value type variables can be assigned a value directly. They are derived from the
class System.ValueType. The value types directly contain data. Some examples are int, char,
and float, which stores numbers, alphabets, and floating point numbers, respectively. When you
declare an int type, the system allocates memory to store the value.
• Reference types: The reference types do not contain the actual data stored in a variable, but
they contain a reference to the variables.
• Pointer types: Pointer type variables store the memory address of another type. Pointers in C#
have the same capabilities as the pointers in C or C++.

Syntax:

<data type> <variable name> = <value>;

The followings are naming conventions for declaring variables in C#:

➢ Variable names must be unique.


➢ Variable names can contain letters, digits, and the underscore _ only.
➢ Variable names must start with a letter.
➢ Variable names are case-sensitive, num and Num are considered different names.
➢ Variable names cannot contain reserved keywords.

The most common data types are as below:

To get the exact size of a type or a variable on a particular platform, you can use the sizeof() method.
data_type.MaxValue() method gives the numerical value the variable will take the most.
data_type.MinValue() method gives the numerical value the variable will take the least.

✓ Integral numeric types

C# type Range Size .NET type


sbyte -128 to 127 Signed 8-bit integer System.SByte
byte 0 to 255 Unsigned 8-bit System.Byte
integer
short -32,768 to 32,767 Signed 16-bit System.Int16
integer
ushort 0 to 65,535 Unsigned 16-bit System.UInt16
integer
int -2,147,483,648 to Signed 32-bit System.Int32
2,147,483,647 integer
uint 0 to 4,294,967,295 Unsigned 32-bit System.UInt32
integer
long -9,223,372,036,854,775,808 Signed 64-bit System.Int64
to integer
9,223,372,036,854,775,807
ulong 0 to Unsigned 64-bit System.UInt64
18,446,744,073,709,551,615 integer

✓ Floating-point numeric types

float ±1.5 x 10−45 to ±3.4 x 4 bytes System.Single


1038
double ±5.0 × 10−324 to ±1.7 × 8 bytes System.Double
10308
decimal ±1.0 x 10-28 to ±7.9228 x 16 bytes System.Decimal
10 28

The precision of a floating point value indicates how many digits the value can have after the decimal
point. The precision of float is only six or seven decimal digits, while double variables have a precision
of about 15 digits. Therefore it is safer to use double for most calculations.

✓ Boolean value

bool true-false (Default false) 16 bit System.Boolean

✓ Textual expressions

char U+0000 to U+FFFF 16 bit System.Char


string System.String

int d = 3, f = 5; /* initializing d and f. */

byte z = 22; /* initializes z. */


double pi = 3.14159; /* declares an approximation of pi. */

bool myBool = true;

char x = 'x';

String str = "Nesrin ATASOY";

Ex : Types

• Defining Constants
Constants are defined using the const keyword.
Syntax:

const <data_type> <constant_name> = value;


Ex : const data
using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
const double pi = 3.14159;

// constant declaration
double r;
Console.WriteLine("Enter Radius: ");
r = Convert.ToDouble(Console.ReadLine());

double areaCircle = pi * r * r;
Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
Console.ReadLine();
}
}
}

• Local/Global Variables:
A scope in any programming is a region of the program where a defined variable can have its existence
and beyond that variable it cannot be accessed. Let us understand what are local and global variables.
➢ Inside a function or a block which is called local variables. A local variable is used where the
scope of the variable is within the method in which it is declared. They can be used only by
statements that are inside that function or block of code.
using System;
public class Program
{
public static void Main()
{
int a;
a = 100;
// local variable
Console.WriteLine("Value:" + a);
}
}

➢ Outside of all functions which is called global variables. A global variable is a variable
accessible anywhere, for example a field "counter" type integer. The global variable can be
accessed from any function or class within the namespace.

C# is an object-oriented programming (OOP) language and does not support global variables
directly. You need to add the static keyword before class and type.

Homework 1:

using System;

namespace ConsoleApp1
{
class Bread
{
public int counter;
}

static class Butter


{
public static int counter;
}

class Program
{
static void Main(string[] args)
{
// instance of class "Butter" required
Bread a = new Bread();
a.counter = 5;
Console.WriteLine("Bread: " + a.counter);

// no instance required!
Butter.counter = 7;
Console.WriteLine("Butter: " + Butter.counter);
}
}
}

You might also like