Declaration:-
YourDatatype
yourvariablename = Value;
(or)
YourDatatype
yourvariablename;
- Assign/change value of variable
Yourvariablename =
value;
- print the value:
System.console.WriteLine(Yourvariablename);
Ex:-
Int
x = 10;
x
= 70;
note:-
system allocates n bytes of memory for the
variable x.
Datatype:-
It
is a keyword, used to specify type of the value that can be stored a variable.
Ex: int, string etc
Types
of Datatypes:-
Numarical datatypes ==> Used to store numbers.
Non- Numarical
datatypes ==> Used to store chars, strings.
Predefined
Datatypes:-
1.
Numarical datatypes:-
S.No
|
Datatype
|
Description
|
Range
|
No of Bytes
|
1
|
sbyte
|
8-bit signed integer
|
-128 to 127
|
1
|
2
|
byte
|
8-bit
unsigned integer
|
0 to 255
|
1
|
3
|
short
|
16-bit signed integer
|
-32768 to
32767
|
2
|
4
|
ushort
|
16-bit
unsigned integer
|
0 to 65535
|
2
|
5
|
Int
|
32-bit signed integer
|
-2147483648 to
2147483647
|
4
|
6
|
uint
|
32-bit
unsigned integer
|
0 to
4294967295
|
4
|
7
|
long
|
64-bit signed integer
|
-9223372036854775808
to 9223372036854775807
|
8
|
8
|
ulong
|
64-bit
unsigned integer
|
0 to 18446744073709551615
|
8
|
9
|
float
|
Signed floatingpoint no
|
1.5*10^-45 to 3.4*10^38
|
4
|
10
|
double
|
Signed
floatingpoint no
|
5*10^324
to 1.7*10^308
|
8
|
11
|
decimal
|
Signed floatingpoint no
|
+-1.0*10^28 to
+-7.9*10^28
|
10
|
2.
Non- Numarical datatypes:-
S.No
|
Datatype
|
Description
|
No of Bytes
|
1
|
char
|
To store characters only
|
2
|
2
|
string
|
To
store single (or) multiple characters
[Upton
1 billion char]
|
(No
of chars)*2
|
3
|
bool
|
To store a Boolean value
[true / false]
|
1
|
Ex:-
using
System;
namespace
DataTypesDemo
{
class Program
{
static void Main(string[] args)
{
char Gender;
Gender =
'm';
bool IsMarried;
IsMarried = false;
string PersonName;
PersonName = "vishnu";
Console.WriteLine(Gender);
Console.WriteLine(PersonName);
Console.WriteLine(IsMarried);
Console.ReadKey();
}
}
}
Operators:-
These are the
symbols to perform certain operations.
Ex:- *, +, -, / etc.
Note:- All operators are same as ‘C’ language.
Control
Statements:-
- These are used to execute the statement conditionally.
- Types of control statements:
·
If
-
Sample if
-
If else
-
Else if
-
Nested if
·
Switch – case
·
While
·
Do-while
·
For
·
Goto
·
Break
·
Continue
Note:- All syntaxes are same as ‘C’ language.
Constants:-
Used
to declare fixed values.
Syntax:-
Const
datatypename = value;
Ex:- const
decimal pi = 3.14;
Note:-
Const
values can’t be changed at runtime.
Parsing:-
It
is used to convert a string value into any required datatype.
Syntax:-
Yourdatatype.parse(value);
Ex:- int.parse(“700”);
Ex:-
using
System;
namespace
ParsingDemo
{
class Program
{
static void Main(string[] args)
{
Console.Write("enter
salary");
decimal Salary = decimal.Parse(Console.ReadLine());
decimal Tax = Salary * 7.6M / 100;
Console.Write("tax
is:"+Tax);
Console.ReadKey();
}
}
}
Variable
Naming Rules:-
- No duplicate variable names are allowed.
- No keywords can be used as variable names.
- No spaces are allowed in variable names.
Ex:-
Int
xyz; // valid
String
void;// not valid
Casting:-
It
is used to convert a value from any datatype to any other required datatype.
Syntax:-
(datatype)value;
Ex:-
using
System;
namespace
CastingDemo
{
class Program
{
static void Main(string[] args)
{
int Marks = 70;
byte b;
b = (byte)Marks;
Console.WriteLine(b);
Console.ReadKey();
}
}
}
Conversion
methods:-
- It is used to convert a value from any standard datatype to any other required standard datatype.
- It is done using some predefined conversion methods provided by system.convert class.
- Ex:-
Int
x = 10;
Double
d = System.Convert.ToDouble(x);
Ex:-
using
System;
namespace
ConvertionMethodDemo
{
class Program
{
static void Main(string[] args)
{
string str = "100";
int x = Convert.ToInt32(str);
double y = Convert.ToDouble(str);
//bool z = Convert.ToBoolean(str); (error)
decimal z = Convert.ToDecimal(str);
Console.WriteLine(x+"\n"+y+"\n"+z);
Console.ReadKey();
}
}
}
List of Conversion methods in C#:-
- System.Convert.ToSBite(value) ==> Converts any value into ‘SByte’
- System.Convert.ToBite(value) ==> Converts any value into ‘Byte’
- System.Convert.ToInt16(value) ==> Converts any value into ‘Short’
- System.Convert.ToUInt16 (value) ==> Converts any value into ‘UShort’
- System.Convert.ToInt32(value) ==> Converts any value into ‘Int’
- System.Convert.ToUInt32(value) ==> Converts any value into ‘UInt’
- System.Convert.ToInt64(value) ==> Converts any value into ‘Long’
- System.Convert.ToUInt64(value) ==> Converts any value into ‘ULong’
- System.Convert.ToSingle(value) ==> Converts any value into ‘float’
- System.Convert.ToDouble(value) ==> Converts any value into ‘Double’
- System.Convert.ToDecimal(value) ==> Converts any value into ‘Decimal’
- System.Convert.ToChar(value) ==> Converts any value into ‘Char’
- System.Convert.ToString(value) ==> Converts any value into ‘String’
- System.Convert.ToBoolean(value) ==> Converts any value into ‘Bool’
No comments:
Post a Comment