Declaring Variable in C#:
Variable: These are hte place holders for maintaining the value based on the datatypes used for defining the variable.
Syntax:
[modifier]DataType VariableName[=value];
Modifier:
These are used to specify the access privileges for accessing the members.
Observation:
With in the same program public internal & protected internal between the same way but if these members are accessed from other programs then internal will behave like protected modifiers.
Data Type:
It is used to specify the type of value that can be assigned for the members C# supports two types of datatypes
>Value Type Datatypes
>Reference Type Datatypes
Boxing:
whenever a value type member is assigned to the reference type member then it is said to be boxing.
Note:
Boxing happens implicitly
Un Boxing:
Whenever a reference type member is assigned to the value type member the it is said to be Unboxing
Note:
Unboxing needs to be performed explicitly
Syntax:
VTVariable =(VTDatatype)RTVariable;
Program:
// Demo on Boxing and Unboxing
Using System;
namespace fresherslike
{
class BoxingNunboxingDemo
{
public static void Main()
{
int i=10; // Value type member
Object P; // Reference type member
P=i; // Boxing
int j; // Value type membe
// j=P; error
j=(int)P; // Unboxing
int k; // Value type member
k=i; // assignement
Console.WriteLine("{0},{1},{2},{3}",i,j,k,P);
}
}
}
Output: 10,10,10
Parsing:
Whenever a string has to be converted to any other datatype value then parsing should be used.
Syntax:
DataType.Parse(stringValue)
Program:
// Demo on Parsing
using System;
namespace Fresherslike
{
class ParsingDemo
{
public static void Main()
{
int Empno;
string Ename;
decimal salary;
DateTime Hiredate;
Console.Write("Enter employee no:");
Empno=int.Parse(Console.ReadLine());
Console.Write("Enter employee Name:");
Ename=Console.ReadLine();
Console.Write("Enter Salary:");
salary=decimal.Parse(Console.ReadLine());
Hiredate=DateTime.Now; //assign the current system date & time
Console.clear(); // clear the screen
Console.ForegroundColor=ConsoleColor.Green; // set the fore color of text
Console .WriteLine("Employee no#:{0} \n Employee Name:{1}\n Salary:Rs{2}\n Date of joining:{3}", Empno, Ename,salary,Hiredate);
}
}
}
Output:
Enter Employee #:101
Employee Name: Fresherslike
Salary: Rs 20000
Date f joining :19/05/2013
Type Casting:
Whenever a lower range value is converted to a higher range value (or) viceversa then it is said to be Type casting
Note:
Type casting will always be performed on the same family type(byte,int,float,double,decimal etc)
Syntax:
VTVariable=(VTDatatype)<VTVariable/VTExpr.>
Program:
// Demo On Type Casting
using System;
namespace Fresherslike
{
class TypeCastingDemo
{
public static void Main()
{
int no1,no2;
float result1,result2;
Console.WriteLine("Enter Two Numbers Below:");
no1= int.Parse(Console.ReadLine());
no2= int.Parse(Console.ReadLine());
result1= no1/no2;
result2=(float)no1/no2; // Type Casting
Console.WriteLine("{0}/{1}={2}", no1,no2,result1);
Console.WriteLine("{0}/{1}={2}", no1,no2,result2);
}
}
}
Input:
10
3
Output:
10/3 = 3
10/3 = 3.3333
<< PREVIOUS NEXT >>
Saturday, 18 May 2013
Subscribe to:
Post Comments (Atom)
awesome tutorials.. really its just like corporate training. please provide more programs and also sample projects, tasks. also provide easy site to compile c# programs online
ReplyDeleteoK anusha definitely I will do it.....
ReplyDeletey are you not posting next classes?
ReplyDeleteDue to some work I am unable to post..... soon i am going to do it...
ReplyDelete