Data Types as Classes

 In .NET, each data type is associated with a class, which offers few methods for the variable (object).
 All of the data type classes are the sub classes of an ultimate base class called “Object”.
 All the data type classes are the members of “System” namespace.
 You can see the data types along with the respective class names in the following table.
Data Type Class Name
sbyte System.SByte
byte System.Byte>
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
bool System.Boolean
char System.Char
decimal System.Decimal
string System.String

 The “Object” class, which is a base class for all other data type classes, offers few methods, which can be accessible by the instances of data type classes (variables).
 You can observe the methods offered by “Object” class in the following table.
Method Description
Equals(value) Checks the equality of the value of the object with the given argument value. If both are equal, then returns “true”, otherwise returns “false”. Ex: MyObj.Equals(AnotherObject); In this example, “MyObj” will be compared with “AnotherObject”.
GetType() Gets the name of the class, for which, it is declared. Ex: MyObj.GetType();
ToString() Converts the value of the object and returns it. Ex: MyObj.ToString(); In this example, the value of “MyObj” will be converted as string type, and that string value will be returned.

Demo on “Object” class methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectClassMethods
{
class Program
{
static void Main(string[] args)
{
int x = 100;
int y = 100;
Console.WriteLine("x is " + x);
Console.WriteLine("y is " + y);
if (x.Equals(y))
Console.WriteLine("x is equal to y");
else
Console.WriteLine("x is not equal to y");
Console.WriteLine("x is the type of " + x.GetType());
string s = x.ToString();
Console.WriteLine("s is " + s);
Console.Read();
}
}
}

Constants

 Constants look just like variables, but concept wise, they will be differ from variables.
 The main differences between constants and variables are:
 The value of constants can’t be changed during the program execution time.
 The constants should be initialized at the time of its declaration.
 To declare constants,
Syn: const datatype constantname=value;
Ex: const int x=100;
Demo on Constants

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Constants
{
//An application, that calculates area of circle
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
Console.WriteLine("Enter r value:");
int r = Convert.ToInt32(Console.ReadLine());
double area = PI * r * r;
Console.WriteLine("Are of circle is " + area);
Console.Read();
}
}
}

Variable Scopes

 A “variable scope” simply is something, which decides the lifetime of the variable.
 The variable scope depends on the place, where the variable is declared in the program.
 For example, a variable is declared in “if” block, is available only within the “if” block itself.
 To have a better idea on this, we discuss about all available scopes in C#.
Scope Description Description Example
1) Class level scope A variable declared within the class. This is known as data member, which accessibility is based on the access modifier you use. (like private, public etc.) class sample
{
private int x;
}
2) Method level scope A variable declared within the method Accessible only within the method only. void sample()
{
int x;
//some code
}
3) Block level scope A variable declared within a block. Ex: if block, else block, for block, try block, catch block etc. Note: A block can be specified when you use curly braces. { } Accessible only within the block only. if (x==y)
{
int x;
//some code
}

 Strong Rule to follow: Multiple variables with same name within the same scope can’t be declared anywhere.

Operators

C# supports different kinds of operators.

 Assignment operator
=
 Arithmetical / Mathematical operators
+, -, *, /, %
 Self Assignment operators
+=, -=, *=, /=
 Prefix and Postfix operators
++, --
 Relational operators
==, !=, <, >, <=, >=
 Logical operators
&&, ||, !
 Comment operators
//xxxxxxxxxxxxxx
(or)
/* xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx*/
 Concatenation Operators
+
 Block specification operators
{ }
 Method specification operators
( )
 Conditional operator
? :
 Dynamic memory allocation operator
new
Operator Precedence:

Flow Control with Control Statements

 C# supports several types control statements.
 These are almost all similar to C++.

Types of Control Statements
 Branching Control Statements
 if
 switch-case
 break
 continue
 goto
 Looping Control Statements
 while
 do-while
 for

Note: All the syntaxes are same as C/C++. Just for idea, we recollect the syntaxes now.

Implementation Syntax of Control Statements

 Branching Control Statements
 if
 Simple if
if (condition)
{
----;
----;
}

 if-else
if (condition)
{
----;
----;
}
else
{
----;
----;
}

 else-if
if (condition)
{
----;
----;
}
else if (condition)
{
----;
----;
}
else if (condition)
{
----;
----;
}
else
{
----;
----;
}

 Nested-if
if (condition)
{
if (condition)
{
----;
----;
}
else
{
----;
----;
}
}
else
{
if (condition)
{
----;
----;
}
else
{
----;
----;
}
}

 switch-case
switch (variable)
{
case value1: ---------; break;
case value2: ---------; break;
case value3: ---------; break;
case value4: ---------; break;
default: ---------; break;
}

 break
for loop / while loop / do-while loop
{
----------;
----------;
break;
----------;
}
 continue
for loop / while loop / do-while loop
{
----------;
----------;
continue;
----------;
}

 goto
----------;
----------;
----------;
goto labelname;
----------;
----------;
labelname:
----------;
----------;
----------;

Looping Control Statements

 while initialization;
while (condition)
{
----------;
----------;
----------;
Increment / decrement;
}
 do-while

initialization;
do
{
----------;
----------;
----------;
Increment / decrement;
} while(condition);
 for
for (initialization; condition; increment/decrement)
{
----------;
----------;
----------;
}

Note: You can also implement nested loops as you implemented in C/C++.