You are on page 1of 10

C# Notes

C# (see sharp) is also a major component of a software package of visual Studio.NET. It has its existence from year 1999. The main purpose of c# is to overcome its counter part java. It provides same sort of platform independencies as provide by java. In the case of java, the source code (the actual program code) is converted into byte code instead of the object code (the machine level equivalent of the source code).the byte code is intermediate program code which is same for all the platform. Similarly, in case of c# the source code is converted into MSIL code(Microsoft intermediate language). It is not the native machine code. This code is similar for all the platform and provides the cross platform operatibility. Creating the console application in C# 1. Click on the Start button, from the start menu, select the All Programs option, in the programs submenu, select the Microsoft Studio .Net 200, and from its submenu select Microsoft Visual Studio .Net 2003.

2. The Microsoft Development Environment window, will appear on the screen.

3. Now , to create a new project of the ASP .Net , from the File menu , select the New option and from the new submenu ,click on the Project option.

4. The New Project dialog box will appear on the screen

. The New Project dialog box , select the Visual C# projects as the project type and in the Templates section of the dialog box , select the Console Application . And in the Name text box , type the project name as Welcome and specify d:\vcsharp as the location where the project directories will get created. 5. The class1.cs class will get created on the screen , in which we can type our program code.

The default body structure of the class1.cs will be , using System;

namespace Welcome { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // } } } using keyword is used for importing the Namespace. Namespace is the technical term given for the collection of predefined or user-defined classes. namespace keyword will be used for defining the user-defined namespace. By default the each class will have the namespace with same name that of the project name. Actually , the physical implementation of the user-defined namespace is that we have the folder with the same name of the namespace created on our hard disk drive. System namespace is default namespace which contains the pre-defined data types.

class Class1

class keyword is used to define the class and Class1 is the default class name.

static void Main(string[] args) { // // TODO: Add code to start application here // } Each program of c# must contain at least one method that is Main , which contains the entire logic of the program. It takes the string args[] as an argument, which will be used for dealing with the command line arguments. string is a predefined class present in the namespace System, used for dealing with the group of characters. Data types in C# Data types of the variables specify the type of value we can store in them. The commonly used data types are,

Data type int long char string float double

Size in Bytes 4 8 2 Approx 1 gb 4 8

Size in bits 32 64 16 32 64

The general form of declaring the variable is , Datatype variablename;

e.g.

int a; char c; string s; float f; double d; long l;


Console : Console class defined in the namespace System, is used for interacting with the console, the console is the technical term refers to the combination of the monitor and keyboard, or in other words we can say that the dos based input and output. The method of the Console class, Method name Read() Description It is used to read a single character. The general form is , int Read(); It will return the ASCII value of the character which we have read from the console.

ReadLine()

This method is used to read a string. The general form is ,

string ReadLine(); Write() It will be used to display information on the screen and on the same line. The general form is , void Write(); e.g. Console.Write("Hello"); Console.Write("World"); Output : WriteLine() HelloWorld It will be used to display information on the screen and will display it on the new line The general form is, void WriteLine(); e.g. Console.WriteLine("Hello"); Console.WriteLine("World"); Output : Hello World To Compile the C# code, Build Menu - Build Project name

To run press - button on the debugging toolbar.

Q1. WAP to read your name and surname. using System; namespace q1 { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // string fname,lname; //read the name and surname Console.Write("Enter the First Name :"); fname=Console.ReadLine(); Console.Write("Enter the Last Name :"); lname=Console.ReadLine(); Console.WriteLine("First Name :"+fname); Console.WriteLine("Last Name :"+lname); Console.ReadLine();

} } } Q2 WAP to read two numbers and display their sum using System; namespace q2 { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // int a,b,c; //read the two numbers Console.Write("Enter the value of a:"); a=int.Parse( Console.ReadLine());

Console.Write("Enter the value of b:"); b=int.Parse(Console.ReadLine());

//add two numbers c=a+b; Console.WriteLine("Sum ="+c); Console.ReadLine(); } } }

You might also like