Introduction to the C# Language
C#
is an object-oriented language that enables developers to build a variety of
secure and robust applications that run on the .NET Framework. C# can be used
to create Windows client applications, client-server applications, database
applications, and much more. Visual C# provides an advanced code editor,
convenient user interface designers, integrated debugger, and many other tools
to make it easier to develop applications based on the C# language and the .NET
Framework. C# was developed by Anders Hejlsberg and his team during the
development of .Net Framework.
C#
syntax is highly expressive, yet it is also simple and easy to learn. The
curly-brace syntax of C# will be instantly recognizable to anyone familiar with
C, C++ or Java. Developers who know any of these languages are typically able
to begin to work productively in C# within a very short time. C# syntax
simplifies many of the complexities of C++
As
an object-oriented language, C# supports the concepts of encapsulation,
inheritance, and polymorphism. All variables and methods, including the Main
method, the application's entry point, are encapsulated within class
definitions. A class may inherit directly from one parent class, but it may
implement any number of interfaces.
C++
vs C#
|
C++ |
C# |
|
|
1) |
C++ is a general purpose, case-sensitive, free-form programming
language that supports object-oriented, procedural and generic
programming. |
C# is pronounced as "C-Sharp". It is an
object-oriented programming language provided by Microsoft that runs on .Net
Framework. |
|
2) |
In C++, multiple inheritance is possible. |
In C#, multiple inheritance is not possible. |
|
3) |
In C++, memory management is handled manually. |
In C#, memory management is handled automatically. |
|
4) |
In C++, pointers can be used anywhere in a program. |
In C#, pointers can be used only in unsafe mode. |
|
5) |
C++ programming is based on OOPs concept. |
C# programming is based onComponent and OOPs concept. |
|
6) |
C++ is a programming language that runs on all platforms. |
C# is a programming language that rarely used outside Windows. |
|
7) |
C++ programming can be used to createconsole applications.
|
C# programming can be used to createconsole applications,
Windows applications, Mobile applications, etc. |
A
namespace is designed for providing a way to keep one set of names
separate from another. The class names declared in one namespace does not conflict
with the same class names declared in another.
Defining a Namespace
A
namespace definition begins with the keyword namespacefollowed by
the namespace name as follows:
namespace namespace_name { // code
declarations }To call the namespace-enabled version of either function or
variable, prepend the namespace name as follows:
namespace_name.item_name;
The using Keyword
The using keyword
states that the program is using the names in the given namespace. For example,
we are using the Systemnamespace in our programs. The class Console
is defined there. We just write:
Console.WriteLine
("Hello there");We could have written the fully qualified name as:
System.Console.WriteLine("Hello
there");You can also avoid prepending of namespaces with the usingnamespace
directive. This directive tells the compiler that the subsequent code is making
use of names in the specified namespace.
The class Keyword
The
class keyword is used for declaring a class.
Comments in C#
Comments
are used for explaining code. Compilers ignore the comment entries. The
multiline comments in C# programs start with /* and terminates with the
characters */ as shown below:
/*
This program demonstrates
The
basic syntax of C# programming
Language
*/
Single-line
comments are indicated by the '//' symbol. For example,
}//end
class Rectangle
Member Variables
Variables
are attributes or data members of a class, used for storing data. In the
preceding program, the Rectangle class has two member variables named length
and width.
Member Functions
Functions
are set of statements that perform a specific task. The member functions of a
class are declared within the class. Our sample class Rectangle contains three
member functions: AcceptDetails, GetArea and Display.
Identifiers
An
identifier is a name used to identify a class, variable, function, or any other
user-defined item. The basic rules for naming classes in C# are as follows:
A
name must begin with a letter that could be followed by a sequence of letters,
digits (0 - 9) or underscore. The first character in an identifier cannot be a
digit.
It
must not contain any embedded space or symbol such as? - + ! @ # % ^ & * (
) [ ] { } . ; : " ' / and \. However, an underscore ( _ ) can be used.
It
should not be a C# keyword.
C# Keywords
Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.


No comments:
Post a Comment