An Overview of C#

C# is a new programming language specifically designed for Microsoft .NET Framework. C# is significant in two respects.

It is specifically designed for use with Microsoft’s .NET Framework.

It is based on modern Object Oriented Design methodology.

An Overview C# and the .NET Platform

C# and the .NET Platform are closely coupled. In fact, every application you create will begin with a connection to the .NET Framework-the developer access portion of the .NET Platform. Yes, you can write unmanaged code under certain circumstances, but most people are going to want to use C# for its intended purpose of writing distributed applications. The easiest way to write managed code with C# is to use the .NET Framework.

C# uses a more extensive list of .NET Framework services for even a small Windows application, as shown here:

1

2

3

4

5

6

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.WinForms;

using System.Data;

As you can see, there’s a tight connection between C# and the .NET Framework. You’ll find that using the .NET Framework for your applications makes coding almost simple. As a result, the development cycle for C# projects is going to be much shorter than Visual C++ projects, once your development staff becomes familiar with the product.

Example C# Program

Let’s start with the traditional way of compiling and running a sample C# program.

1

2

3

4

5

6

7

8

9

10

11

12

13

using System;

namespaceMycplus.CSharpBasics

{

classMyFirstCSharpClass

{

staticvoidmain()

{

Console.WriteLine("This is my First C# program!");

return;

}

}

}

Here first two lines are namespaces which is a way to group associted classes together. This concept is familiar to Java and C++ Programmers, but it will be new to VB programmers. namespace keyword declatres the namespace our class should be associated with. and using statements tell compiler to go through the Syatem name space and find any relavant classes that are accessed from our class.

So here in our programme we are using the Console class’s method WriteLine which is present in the System namespace.

Next we declare the class named MyFirstCSharpClass. The class declaration syntax is just like Java and C++. we declare the class with class keyword and then name of the class. And in the braces we write the definition and methods of the class.

Then comes the methods. Its just like Java and C++. Method definition of C# is like

1

2

3

4

[modifires]return_type MethodName([parameters])

{

//Method Body

}

Variables & Constants

We declare variable in C# as

1

2

3

datatype VariableName;

i.e.

intMyVar;

and we can initialize it as

int MyVar = 100;

There are three main rules to determine the variable scope.

If the variable is declared in the a loop then it scope will be limited to that loop.

If it is declared with in the code block like method then its scope will be finished when the code block ends.

And if it is the class variable then its scope will be limited to that class.

Constant is a variable whos value can not be changed during the program. We can declare the constant in C# as

1

constinti=10;

Benefits of Using C#

The fact is that C# has a lot to offer developers from a personal perspective. I wouldn’t say any of these features are earth shattering, and you’ve probably heard these promises for other languages in the past. C# actually delivers on these promises and makes life easier for the developer. Will C# make you a better programmer? Yes, in some ways. For example, it helps you catch more errors before they become problems. However, only experience creates great programmers.

Most developers will find that C# makes them faster and more efficient programmers. You’ll spend less time figuring out how to code something due to an ambiguity in the development environment. In many cases, you’ll also find that you spend less time searching for just the right API call or switch that isn’t documented.

Now that you have some idea of what you have to gain from C#, let’s discuss the topic in detail. The following sections answer the question, “What will C# do for me?” You’ll learn why C# is such an important improvement in the development community.

Simplicity

C# adds simplicity to the development environment in several ways. We’ve already talked about how Microsoft has simplified the data types by consolidating some and adding others. For example, you no longer need to worry about which char data type to use-there’s only one. Likewise, the new decimal data type will greatly reduce programming complexity. The use of the .NET Framework will simplify matters as well. The hierarchical structure of namespaces will make it easier to find methods that you need. In addition, more system resources are available as methods, rather than API calls. For example, with C# you can now write event log entries using a simple method call, rather than using the convoluted method for Visual C++.

Managed environments simplify programming tasks. For example, C# will greatly reduce memory leaks using garbage collection. Of course, the use of garbage collection will also reduce debugging time because the programmer will have fewer errors to find. Since memory leaks are notoriously difficult to find and fix, most developers will find that garbage collection greatly reduces the amount of debugging time for a given project.

Object Orientation

Unlike Visual C++ and Visual Basic, C# provides true object orientation. Everything is an object. This language does make some concessions for the sake of performance, but even in these areas, C# provides the means to use objects. For example, C# stores and uses variables in the same way that Visual C++ and Visual Basic have in the past. The difference is that C# can box a variable within an object, making it easy to access the variable as an object when needed. This means that C# provides the same level of object orientation that languages like Eiffel and Smalltalk do, but without the performance penalties.

C# also embraces the COM+ virtual object system. This means that all objects execute within a context. You can assign role-based security to objects, which means that you can allow user interaction based on the role the user has within an organization. In short, you have much finer control over the component execution environment.

Finally, C# gets rid of the global variables, methods, and constants. Everything appears within a class. This means there are fewer chances for naming conflicts and the data contained within variables remains safer. While it will take some developers time to get used to the new method of handling data, the result is more reliable components and applications.