You are on page 1of 19

First C# Program

•First c# program
•Printing values
•General form of WriteLine()
•Function, Class and Namespace
•Type of comments.
Introduction to C#
Alphabets,Digits,,Sp symbols

Constants,Variables,Keywords

Statements or Instructions

Programs
• A program is a set of instructions to perform a
specific task.
• Programming languages use programs to
develop software applications.
• A compiler is a special program that processes
the statements written in a particular
programming language and converts them into a
machine language.
• This process of conversion is called compilation.
Tips to write program
All variable must be declared.
 +,-,*,/ are Arithmetic Operators
 to perform multiplication be must use * instead
of x .
For example 2* 3 is correct
 2 x 3 is wrong.
2\3 is not used for division instead of that 2/3 is
used.
A C# program is nothing but a collection of one or more
namespaces.
Inside each namespaces there can be multiple classes, in
short namespaces acts as container which can hold
several classes together.
Each class can contain multiple functions.
Every single class has to belong to some or the other
namespace.so the outer most container is namespace
inside which a smaller container is a class ,inside which a
smaller container called a function and inside the
function there are variables.
The namespaces,class,public,static,void all are
keywords.
C# is case-sensitive and a free form language.
While defining Main() we have to write
as public static void Main()
• C#, also known as C-Sharp, is a programming language
introduced by Microsoft.
• C# is specially designed to work with the Microsoft’s .NET
platform.
Let us understand the structure of a C# program.
Consider the following code example, which defines a class:
public class Hello
{
public static void Main(string[] args)
{
System.Console.WriteLine("Hello, World! \n");
}
}
Classes in C# (Contd.)

public class Hello The class Name


{ Is used as an identifier for
public static void a class
Main(string[] args)
{
System.Console.WriteLine("He
llo, World! \n");
}
}
Classes in C# (Contd.)

public class Hello System.Console.WriteLine()


{ Displays the enclosed text on the
public static void screen
Main(string[] args)
{

System.Console.WriteLine("He
llo, World! \n");
}
}
Accepting and Storing Values in Member Variables

To understand how to accept value in a Console.ReadLine()


variable, let us consider the following code Is used to accept input
snippet: from the user and store it
int Number; in the variable
Number=
Convert.ToInt32(Console.ReadLine());
Writing and Executing a C# Program

Let us know learn to write, compile, and execute a C#


program.
Creating a Sample C# Program

• A C# program
{ can be written by using an editor like Notepad.
ConsiderConsole.WriteLine("Enter
the following code, whichthe Engine
declares Model");
a class Car and
Engine
also creates an=object
Console.ReadLine();
MyCar of the same class:
Console.WriteLine("Enter the number of Wheels");
using System; The using keyword is used to include
NoOfWheels = Convert.ToInt32(Console.ReadLine());
class Car the namespaces in the program.
}
{ Comments are used to explain the
public void DisplayDetails()
//Member variables code and are represented by //
{
string Engine; symbols.
Console.WriteLine("The Engine Model is:{0}",
int NoOfWheels; Member variables are used to store
Engine);
//Member functions the data for a class.
Console.WriteLine("The number of wheels are:{0}",
void AcceptDetails() Member functions are declared inside
NoOfWheels);
} the class that are used to perform a
} specific task.
Creating a Sample C# Program (Contd.)

//Class used to instantiate the Car class


class ExecuteClass The Execute class is used as a class from
{ where the Car class can be instantiated.
public static void Main(string[] args)
{
Car MyCar = new Car();
MyCar.AcceptDetails();
MyCar.DisplayDetails();
}
}
Compiling and Executing C# Program

• After writing the program in a Notepad, you need to compile


and execute it to get the desired output.
• The compiler converts the source code that you write into the
machine code, which the computer can understand.
• The following steps are needed to compile and execute a C#
program.
• 1. Save the code written in the Notepad with an extension
.cs.
• 2. To compile the code, you need to go to the Visual Studio
2005 Command Prompt window. Select StartAll
ProgramsMicrosoft Visual Studio 2005Visual Studio
ToolsVisual Studio 2005 Command Prompt. The Visual
Studio 2005 Command Prompt window is displayed to compile
the program.
• 3. In the Visual Studio 2005 Command Prompt window,
move to the location where the programs file is saved.
Compiling and Executing C# Program (Contd.)

• 4. Compile the program file by using


the following command:
•csc ExecuteClass.cs

• 5. To execute the code, type the


following in the command prompt:
•ExecuteClass.exe
Terminator
In C# semicolon(;) is used to end program
statement.
For example float p,r,si ;
; is known as statement terminator .
We can write multiple line in single line by using
semicolon.
Comments and it’s Types
A comment is a line or paragraph of text that will not be
considered as part of your code of a program. There are two
types of comments recognized by C#.
To display a comment on a line of text, start the line with two
forward slashes //. Anything on the right side of // would be
ignored. Here is an example:
// This line will be ignored. I can write in it anything I want
The above type of comment is used on only one line. You can
also start a comment with /*. This type of comment ends with
*/. Anything between this combination of /* and */ would not
be read. Therefore, you can use this technique to span a
comment on more than one line.
Tip about comments
Any number of comments are allowed anywhere in
program.
Avoid writing nested comment.

You might also like