C# (pronounced see sharp) is a multi-paradigm programming language
encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented
programming disciplines
It was developed by Microsoft
within its .NET initiative and later approved as a
standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006).
C# is one of the programming languages designed for the Common Language Infrastructure.
C# is intended to be a simple, modern,
general-purpose, object-oriented programming language. Its development team is
led by Anders Hejlsberg. The most recent version is C# 5.0,
which was released on August 15, 2012.
Design goals
The ECMA standard lists these design
goals for C#
- C# language is intended to be a simple, modern, general-purpose, object-oriented programming language.
- The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection. Software robustness, durability, and programmer productivity are important.
- The language is intended for use in developing software components suitable for deployment in distributed environments.
- Source code portability is very important, as is programmer portability, especially for those programmers already familiar with C and C++.
- Support for internationalization is very important.
- C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.
- Although C# applications are intended to be economical with regard to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language.
Advantages over C and C++
- It is compiled to an intermediate language (CIL) independently of the language it was developed or the target architecture and operating system
- Automatic garbage collection
- Pointers no longer needed (but optional)
- Reflection capabilities
- Don't need to worry about header files ".h"
- Definition of classes and functions can be done in any order
- Declaration of functions and classes not needed
- Unexciting circular dependencies
- Classes can be defined within classes
- There are no global functions or variables, everything belongs to a class
- All the variables are initialized to their default values before being used (this is automatic by default but can be done manually using static constructors)
- You can't use non-boolean variables (integers, floats...) as conditions. This is much more clean and less error prone
- Apps can be executed within a restricted sandbox
Advantages over C++ and java
- Formalized concept of get-set methods, so the code becomes more legible
- More clean events management (using delegates)
Advantages over java
- Usually it is much more efficient than java and runs faster
- CIL (Common (.NET) Intermediate Language) is a standard language, while java bytecodes aren't
- It has more primitive types (value types), including unsigned numeric types
- Indexers let you access objects as if they were arrays
- Conditional compilation
- Simplified multithreading
- Operator overloading. It can make development a bit trickier but they are optional and sometimes very useful
- (limited) use of pointers if you really need them, as when calling unmanaged (native) libraries which doesn't run on top of the virtual machine (CLR)
C# example programs
“Hello world!”
using System;
public class HelloWorld
{
public static void
Main(string[] args) {
Console.Write("Hello
World!");
}
}
You
can compile c# using the command line version
C:>csc HelloWorld.cs
and
then run the new program by entering
Output:
HelloWorld
Read
an entire file into a string
using System;
namespace PlayingAround {
class ReadAll {
public static void Main(string[] args) {
string contents = System.IO.File.ReadAllText(@"C:\t1");
Console.Out.WriteLine("contents = " + contents);
}
}
}
Read
all the lines from a file into an array
using System;
namespace PlayingAround {
class ReadAll {
public static void Main(string[] args) {
string[] lines = System.IO.File.ReadAllLines(@"C:\t1");
Console.Out.WriteLine("contents = " + lines.Length);
Console.In.ReadLine();
}
}
}
Example
of Constructors, static Constructor and Destructor
using System;
class Test2
{
static int i;
static Test2() { // a constructor for the entire class called
//once before the first object created
i = 4;
Console.Out.WriteLine("inside static construtor...");
}
public Test2() {
Console.Out.WriteLine("inside regular construtor... i={0}",i);
}
~Test2() { // destructor (hopefully) called for each object
Console.Out.WriteLine("inside destructor");
}
static void Main(string[] args)
{
Console.Out.WriteLine("Test2");
new Test2();
new Test2();
}
}
Output:
inside static construtor...
Test2
inside regular construtor... i=4
inside regular construtor... i=4
inside destructor
inside destructor