Introduction c#:
WHAT IS C# ?
C# (C' sharp) is a computer programming language developed by Microsoft Corporation, USA. C# is a fully object oriented programming language like Java . It is a simple , efficient, productive and type-safe language derived from the popular C and C++ language.- C# is designed for building robust, reliable and durable component to handle real-world applications.
- C# is language derived from the C/C++ family.
- C# is the only language designed for the .NET Framework.
- C# is a concise, lean and modern language.
- C# has a lean and consistent syntax.
CHARACTERISTICS OF C#
- Simple
- Object-Oriented
- Type-safe
- Interoperable
- Felixible
APPLICATION OF C#
C# language can be used for a variety of applications that are supported by the .NET platform.
- Console Applications
- Windows Applications
- Windows Controls
- ASP.NET Projects
- Web Services
FIRST C# PROGRAM
class Firstprogram
{
public static void Main()
{
System.Console.WriteLine(" This is my first C# program");
}
}
write the above program in notepad and save it with .cs file extension in a folder in your system.
Ex.
firstprogram.cs
For compiling the program go to the C# developer command prompt and type the following command :
csc firstprogram.cs
The C# compiler compile your code and create an executable file by name
firstprogram.exe
If there are errors, the compiler will produce appropriate error messages.
For executing the program , simply type the name of the executable file at the command prompt. ie
firstprogram
This will display the output on command prompt.
This is my first C# program
EXPLANATION OF ABOVE EXAMPLE
Class Declaration
the first line class Firstprogram
class is a keyword and declares that a new class definition follows. Firstprogram is a C# identifier that specifies the name of the class to be defined.
The Braces
C# is block structured language , meaning code blocks are always enclosed by braces { and }.
The Main Method
public static void Main()
Every C# executable program must include Main() method in one of the classes. public, static and void are the key.words
public- public keyword is an access modifier that tell the C# compiler that the Main method is accessible by anyone.
static- static keyword declares that the Main method is a global one and can be called without creating an instance of the class.
void- void keyword is a type modifier that states that the Main method does not return any value .
NAMESPACE
System.Consloe.WriteLine();
In the above line System is the namespace in which the Console class is located. A class in a namespace can be accessed by using dot operator.
C# support a features known as using directive that can be used to import the namespace System into the program. once a namespace is imported , we can use the element of that namespace without using the namespace as prefix.
Example:
using System;
class program
{
public static void Main()
{
Console .WriteLine("This is second program");
}
}
In this example we have not used the System prefix to the Console class in the output line.
MAIN RETURNING A VALUE
We have used void as return type in earlier program. Main () can also return a value if it is declared as int type instead of void. When the return type is int, we must include a return statement at the end of the method in the program.
Example:
using System;
class program
{
public static int Main()
{
Console.WritLine("This is my third program");
return 0;
}
}
No comments:
Post a Comment