Introduction to C#
C# (pronounced C-Sharp) is a powerful and flexible programming language.Like all other programming languages, it is used to create a variety applications.
C# is an Object Oriented Programming language and has at its core, many similarities to Java, C++ and VB.In fact, C# combines the power and efficiency of C++, the simple and clean OO design of Java and the language simplification of Visual Basic.
C# removes some of the complexities and pitfalls of languages such as Java and C++.C# also does not allow multiple inheritance or the use of pointers (in safe/managed code), but does provide garbage memory collection at runtime, type and memory access checking. However, contrary to JAVA, C# maintains the unique useful operations of C++ like operator overloading, enumerations, pre-processor directives, pointers (in unmanaged/un-safe code), function pointers (in the form of delegates) and promises to have template support in the next versions.
From the above discussion, objectives of C# can be summarized as :
- Simple
- Modern
- Modular
- Object-oriented
- Powerful and flexible
- A Language of few words
C# is simple and modern because of its extensive features like garbage collection, extensible data types and exception handling.
It is powerful and flexible because it is limited by your imagination.
It is a language of few words ,constants also called as Keywords which serves the built-in functionality.
Keywords, also known as reserved words, are tokens that have special meaning in a language. The Keywords of C# are listed below :
COMMENTS IN C#
C# Coding Style is used to robust and reliable programs.Comments are the programmer's text to explain the code, are ignored by the compiler and are not included in the final executable code. C# uses syntax for comments that is similar to Java and C++. The comment ends with the end of the line.
Block comments:
Block comments should usually be avoided. For descriptions use of the /// comments to give C# standard descriptions is recommended. Block comments can be used in the following style :
/* Line 1
* Line 2
* Line 3
*/Alternatively oldfashioned C style single line comments, even though it is not recommended./* blah blah blah */
Single Line Comments:
These are used for commenting sections of code too.
//Single comment
A rule of thumb says that generally, the length of a comment should not exceed the length of the code explained by too much, as this is an indication of too complicated, potentially buggy, code.
THE PROGRAM-DEVELOPMENT LIFE CYCLE
The program-development cycle has its own steps. In the first step, you use an editor to create a file that contains your source code. In the second step, you compile the source code to create an intermediate file called either an executable file or a library file. The third step is to run the program to see whether it works as originally planned.
Creating The Source Code:
Source code is a series of statements or commands used to instruct the computer
to perform your desired tasks. These statements and commands are a set of key- words that have special meaning along with other text. As a whole, this text is readable and understandable.
Here is a snippet of C# source code:
System.Console.WriteLine(“
This line of source code writes Hello Blogger!
Using An Editor :
An editor is a program that can be used to enter and save source code. A number of editors can be used with C#. Some are made specifically for C#, and others are not.Microsoft has added C# capabilities to Microsoft Visual Studio .NET, which now includes Microsoft Visual C# .NET. This is the most prominent editor available for C# programming.
If you don’t have a C# editor, don’t feel bad. Most computer systems include a program that can be used as an editor. If you’re using Microsoft Windows, you can use either Notepad or WordPad as your editor. If you’re using a Linux or UNIX system, you can use such editors as ed, ex, edit, emacs, or vi.
Naming Your Source Files :
The name should describe what the program does. Although you could give your source file any extension, .cs is recog- nized as the appropriate extension to use for a C# program source file.
Understanding the Execution of a C# Program
C# programs are created to run on the .NET Common Language Runtime (CLR). This means that if you create a C# executable program and try to run it on a machine that doesn’t have the CLR or a compatible runtime, the program won’t execute.With C#, we create only one executable program, and it runs on either machine.
C# compiler produces an Intermediate Language (IL) file. This IL file can be copied to any machine with a .NET CLR.The CLR or a compatible C# runtime does this final compile just as it is needed.Compiling the program is one of the first things the CLR does with an IL file. In this process, the CLR converts the code from the portable, IL code to a language (machine language) that the computer can understand and run. The CLR actually compiles only the parts of the program that are being used. This saves time. This final compile of a C# pro- gram is called Just In Time (JIT) compiling, or jitting.
Compiling C# Source code to Intermediate Language
C# compiler is used to create the IL file.For Microsoft .NET Framework SDK, the csc command is used, followed by the name of the source file, to run the compiler. For example, to compile a source file called Blogger.cs, you type the following at the command line:
csc Blogger.cs
For other frameworks like mono whose compiler is mcs can be compiled as :
mcs Blogger.cs
For graphical development environment such as Microsoft Visual C# .NET, compiling is even simpler. In most graphical environments, compile a program by selecting the Compile icon or selecting the appropriate option from the menu. After the code is compiled, selecting the Run icon or the appropriate option from the menus executes the program.
NOTE : After a program is compiled, an IL file is generated. If you look at a list of the files in the
directory or folder in which you compiled, you should find a new file that has the same name as your source file, but with an .exe (rather than a .cs) extension. The file with the .exe extension is your compiled program (called an assembly). This program is ready to run on the CLR. The assembly file contains all the information that the CLR needs to know to execute the program. According to .NET terminology, the code inside the assembly file is called managed code.
Completing the Development Cycle
A compiled IL file can be run by entering its name at the command-line prompt or just by Run in any other program. However, the program requires a .NET CLR. If the CLR is not installed,an error will be generated when you run the program. Installing the Microsoft .NET Framework allows to run the programs like all other programs. For other frameworks,something different is to be done. For example, when using the mono compiler (mcs), the program can be run by entering it after mono. For example, to run the Blogger program mentioned earlier, you would type the following at the command line:
mono Blogger.exe
Steps to Write a Simple C# Program
Possibly download and install the free Microsoft .NET SDK or Visual Studio.Net.After the .NET SDK is installed successfully, follow these simple steps :
- Choose a Text Editor and type the program in command console.
- Save the file with the .cs extension.
- Compile the Code
- If compiled successfully, a file with .exe extension is created in the same folder.
- Run the program.
- Verify that the output matches the desired one.
Create Your First C# Program
class Blogger
{
public static void Main()
{
System.Console.WriteLine(“Hello, Blogger!”);
}
}
Output : Hello, Blogger!
No comments:
Post a Comment