Data Types

 C# supports several types of predefined data types. You can observe them in the following diagram.
data types
You can observe the data types, memory size and ranges in the following table.
datatypes

Variables

 Variable declaration:
Syn: datatype variable;
Ex: int x;

 Variable declaration along with initialization:
Syn: datatype variable=value;
Ex: int x=10;

 Multiple Variable declaration:
Syn: datatype variable1,variable2,variable3…;
Ex: int x,y,z;

Note: When we declare any variable, it should be assigned to a value before its usage. Otherwise, it causes a compile time error.
Note: “+” is known as “concatenation” operator, which can concatenate two similar or different types of values as a “string”.
Demo on Variables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Variables
{
class Program
{
static void Main(string[] args)
{
int n = 4561;
Console.WriteLine(n);
decimal a, b, c;
double d1, d2=45.123, d3;
Console.WriteLine("d2 value is " + d2);
string s1;
bool b1 = true;
Console.WriteLine(b1);
string Country = "India";
int States = 28;
Console.WriteLine("Our country is " + Country + ". It has " + States + " states.");
Console.Read();
}
}
}

C# Data types

Escape Sequence Characters

Escape Sequence Characters
Demo on Escape Sequence

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

namespace EscapeSequenceCharacters
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is first line\nThis is second line\nThis is third line.");
Console.WriteLine("\'C#\' has derived from \"C\" and \"C++\".");
Console.WriteLine("The escape sequence character \\n is used to insert new line.");
Console.WriteLine("One\tTwo\tThree");
Console.WriteLine("An\bt");
Console.Read();
}
}
}

C# Data types

Type casting

Def: The process of converting the value from one data type to another data type is called as “Casting”.
This is of two types.
1. Implicit Casting:
The value can be converted by the compiler automatically, without using any extra statement.
2. Explicit Casting:
The value can be converted by the programmer using a conversion method.

Implicit Casting
Implicit casting is possible in the following cases.
 Any numerical value from lower to higher type.
Ex: byte to short
short to int
float to double
etc.
 Any numerical value from non-decimal type to decimal type.
Ex: int to float
long to double
etc.
The following table shows the all possible implicit conversion supported by C#.
Implicit Casting in C#:
Implicit Casting in C#
Explicit Casting
Explicit casting should be performed in the following cases:
 Any numerical value from higher type to lower type.
 Any numerical value from decimal type to non-decimal type.
 Any value from numerical type to non-numerical type.
 Any value from non-numerical type to numerical type.
Syn: (target data type)variable
(target data type)value

Demo on Casting using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Casting
{
class Program
{
static void Main(string[] args)
{
int x = 10;
long y = x; //implicit
byte z = (byte)x; //explicit
double p = x; //implicit
int q = (int)p; //explicit
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
Console.WriteLine(p);
Console.WriteLine(q);
Console.Read();
}
}
}

C# Data types

Explicit Casting using Conversion Methods

The “System.Convert” class provides several methods to perform explicit casting.
 System.Convert.ToSByte(value);
Converts the value into “sbyte” type.

 System.Convert.ToByte(value);
Converts the value into “byte” type.

 System.Convert.ToInt16(value);
Converts the value into “short” type.

 System.Convert.ToUInt16(value);
Converts the value into “ushort” type.

 System.Convert.ToInt32(value);
Converts the value into “int” type.

 System.Convert.ToUInt32(value);
Converts the value into “uint” type.

 System.Convert.ToInt64(value);
Converts the value into “long” type.

 System.Convert.ToUInt64(value);
Converts the value into “ulong” type.

 System.Convert.ToSingle(value);
Converts the value into “float” type.

 System.Convert.ToDouble(value);
Converts the value into “double” type.

 System.Convert.ToChar(value);
Converts the value into “char” type.

 System.Convert.ToString(value);
Converts the value into “string” type.

 System.Convert.ToBoolean(value);
Converts the value into “bool” type.


Demo on Casting with Conversion Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConversionMethods
{
class Program
{
static void Main(string[] args)
{
int x = 10;
long y = x; //implicit
byte z = Convert.ToByte(x); //explicit
double p = x; //implicit
int q = Convert.ToInt32(p); //explicit
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
Console.WriteLine(p);
Console.WriteLine(q);
char c = 'A';
string s = Convert.ToString(c); //explicit
Console.WriteLine(s);
Console.Read();
}
}
}
C# Data types
IMP Note:  By default, C# recognizes all the integer constants as the type of “int”.
 In the same way, C# recognizes all the floating point constants as the type of “double”.