C# ??
hey friends!!...
here is the stuff for you.
I hope it will be helpful for you.
One of designer of C# that Wasanders Hejsberg was a developer of java.
So most of the features of java are there in the c#.
C# contained concepts of C language,power that is component orientation of C++ and Elegance of java also the productivity of visual basic.
C# has many applications: Like (a)Console applications (b)Windows application (c) Providing web services (d)creating web controls and many more..
.NET(dot net) is a framework that includes everythng required for developing software for web services developed by microsoft primarily run on windows.
.NET(dot NET):THE C# ENVIRONMENT

It consists of three distict technologies as:(1)common language runtime(2) Framework base classes(3)User and program interfaces.
It has a CLR(Comman Language Runtime),which provide the Interoperability with other languages
Program Structure :
using directive section includes includes all those namespaces that contains classes required in the application.It tells to the compiler to look for the class in that namespace.
Interface are used when we want to implement the concept of multiple inheritance ina a program.
class are included in class section.And last one is the main section method,Main method is the starting point in a c# program and so is essential part.There can be multiple Main methods in a C# program.
EXECUTING THE PROGRAM:
After creating a c program,save it with .cs file extension. Ex: sample.cs
And for compiling it simply type : csc sample.cs
DATA TYPES :
Data types specify the size and the type of values that can be stored.
So,in C# there are two categories of data types:
(a) Value types
(b) Reference types
(a) Value types:
These are stored on the Stack, and when value of a variable is assigned to another,they are actually copied.Hence two identical copies of that variable exits in memory.
hence it refers that two variables with same values in the memory.Value data types are: integers,real numbers, Boolean,characters,Enumerations,structures.
(b)Reference types:
These are stored on heap, so when assignment of two reference variable occurs, only the reference is copied but actual value remains in d same memory location.Which implies that,there are two references to a single value.
reference data types are:objects,strings,classes,arrays,delegates,interfaces.
BOXING AND UNBOXING :
Boxing:
Boxing means conversion of a value type on the stack to a object type on the heap.When compiler finds a value type needs to convert in a reference type,it creates an object 'box'into that it places the value of value type.
Example:
int a=10;
Object b=a; //creates a box rm to hold m
a=20;
Here boxing create a copy of integer a to object b.Now both variables are different where the value of a resides on the stack and that of b on the stack.So,the values are independent of each other.
from above example value of a is 20 and that of b is 10.
Unboxing:
Its the process of converting the object type back to the value type.We can only unbox a variable that has previously been boxed.Its an explicit conversion because during unboxing the object can cast to any type.
Example:
int a=10;
object b=a; //boxing a implicitly
int c=(int)b; //unboxing explicitly
During unboxing,we have to ensure that the value type is large enough to hold the value of object.
A SIMPLE C# PROGRAM :
Program:P1:/*here is very simple program in c#*/
class SimpleProg
{
public static void Main()
{
System.Console.WriteLine("Simple program in c#");
}
}
SO,
C# is an object oriented language therefore everything must be placed inside a class.
Its a block-structured language so,code blocks are alwys enclosed in braces { & }.
Here in c# the M of Main keyword should be capital,Public is an access modifier and static declares that Main method is a global and can be called without creating an instance of the class.
The out put line:
System.Console.WriteLine("Simple program in c#");
where WriteLine is a static method of the Console class,which is located in the namespace System.Also every C# statement must end with a semicolon(;).
And // used for single line commants where /*........*/ used for multiline commants.
USE OF CASTING OPERATION:
Program p2: /*A simple program showing use of casting operation*/
using system; //namespace
class Casting
{
public static void Main()
{
float sum;
int i;
sum=0.0F;
/*value initialized to a real variable must followed by F,otherwise it will be
considered as decimal value*/
for(i=1;i<=10;i++)
{
sum+=1/(float)i; //type casting int to float
Console.Write("i==>"+i);
Console.WriteLine("Sum==>"+sum);
}
}
}
So,
Casting into a smaller type may result in loss of data.
For example from above example if we try to convert the float type into int type,it may result in the loss of fractional part.
using system; //namespace
class Casting
{
public static void Main()
{
float sum;
int i;
sum=0.0F;
/*value initialized to a real variable must followed by F,otherwise it will be
considered as decimal value*/
for(i=1;i<=10;i++)
{
sum+=1/(float)i; //type casting int to float
Console.Write("i==>"+i);
Console.WriteLine("Sum==>"+sum);
}
}
}
So,
Casting into a smaller type may result in loss of data.
For example from above example if we try to convert the float type into int type,it may result in the loss of fractional part.
USING ALIASES FOR CLASS:
Program P2:/*A program using aliases for class and command line arguments*/
using A=System.Console; //A is alis for System.Console
class Sample
{
public static void Main(string[ ] args)
{
A.WriteLine("welcometo"); // Write can also be used in place of WriteLine
A.WriteLine(" ",args[0]); //command Line argument
A.WriteLine(" ",args[1]);
}
}
Command Line aruguments are parameters suplied to the maine method at the time of invoking it for execution.args that is given as the Main method parameter,is an array of strings. Arguments provided in the command line are passed to the arrayargs.
Also,C# is a platform free language, we need not to indent any line.
No comments:
Post a Comment