Sei sulla pagina 1di 4

INTRODUCTION to language

________________________
C# (pronounced "C Sharp") is a multi-paradigm programming language encompassing
imperative, functional, generic, object-oriented (class-based), and component-or
iented programming disciplines. It was developed by Microsoft within the .NET in
itiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23
270). C# is one of the programming languages designed for the Common Language In
frastructure.
C# is intended to be a simple, modern, general-purpose, object-oriented programm
ing language. Its development team is led by Anders Hejlsberg, the designer of B
orland's Turbo Pascal. It has an object-oriented syntax based on C++. The most r
ecent version of the language is 3.0 which was released in conjunction with the
.NET Framework 3.5 in 2007. The next proposed version, 4.0, is in development.

C# Paradigm multi-paradigm: structured, imperative, object-oriented, event-drive


n, functional
Appeared in 2001
Designed by Microsoft
Stable release 3 (19 November 2007)
Typing discipline static, strong, safe, nominative
Major implementations .NET Framework, Mono, DotGNU
Dialects C?, Spec#, Polyphonic C#
Influenced by Object Pascal, C++, Modula-3, Java, Eiffel, C

By design, C# is the programming language that most directly reflects the underl
ying Common Language Infrastructure (CLI). Most of its intrinsic types correspon
d to value-types implemented by the CLI framework. However, the language specifi
cation does not state the code generation requirements of the compiler: that is,
it does not state that a C# compiler must target a Common Language Runtime, or
generate Common Intermediate Language (CIL), or generate any other specific form
at. Theoretically, a C# compiler could generate machine code like traditional co
mpilers of C++ or FORTRAN. In practice, all existing compiler implementations ta
rget CIL.
Some notable distinguishing features of C# are:
_________________________________________________
There are no global variables or functions. All methods and members must be decl
ared within classes. Static members of public classes can substitute for global
variables and functions.
Local variables cannot shadow variables of the enclosing block, unlike C and C++
. Variable shadowing is often considered confusing by C++ texts.
C# supports a strict Boolean datatype, bool. Statements that take conditions, su
ch as while and if, require an expression of a boolean type. While C++ also has
a boolean type, it can be freely converted to and from integers, and expressions
such as if
(a) require only that a is convertible to bool, allowing a to be an int, or a po
inter. C# disallows this "integer meaning true or false" approach on the grounds
that forcing programmers to use expressions that return exactly bool can preven
t certain types of programming mistakes such as if (a =
b) (use of = instead of ==).
In C#, memory address pointers can only be used within blocks specifically marke
d as unsafe, and programs with unsafe code need appropriate permissions to run.
Most object access is done through safe object references, which always either p
oint to a "live" object or have the well-defined null value; it is impossible to
obtain a reference to a "dead" object (one which has been garbage collected), o
r to a random block of memory. An unsafe pointer can point to an instance of a v
alue-type, array, string, or a block of memory allocated on a stack. Code that i
s not marked as unsafe can still store and manipulate pointers through the Syste
m.IntPtr type, but it cannot dereference them.
Managed memory cannot be explicitly freed; instead, it is automatically garbage
collected. Garbage collection addresses memory leaks by freeing the programmer o
f responsibility for releasing memory which is no longer needed. C# also provide
s direct support for deterministic finalization with the using statement (suppor
ting the Resource Acquisition Is Initialization idiom).
Multiple inheritance is not supported, although a class can implement any number
of interfaces. This was a design decision by the language's lead architect to a
void complication, avoid dependency hell and simplify architectural requirements
throughout CLI.
C# is more typesafe than C++. The only implicit conversions by default are those
which are considered safe, such as widening of integers and conversion from a d
erived type to a base type. This is enforced at compile-time, during JIT, and, i
n some cases, at runtime. There are no implicit conversions between booleans and
integers, nor between enumeration members and integers (except for literal 0, w
hich can be implicitly converted to any enumerated type). Any user-defined conve
rsion must be explicitly marked as explicit or implicit, unlike C++ copy constru
ctors and conversion operators, which are both implicit by default.
Enumeration members are placed in their own scope.
C# provides properties as syntactic sugar for a common pattern in which a pair o
f methods, accessor (getter) and mutator (setter) encapsulate operations on a si
ngle attribute of a class.
Full type reflection and discovery is available.
C# currently (as of 3 June 2008) has 77 reserved words.

Modernized language
___________________
C# is a modernized version of C++. Originally you had the language C, which was
widely used. C++ came about to add object-orientation to C, and C++ became the l
anguage of building real applications for Windows (according to the C++ developers
.) C++ was used for writing the infrastructure and low-level applications, while
Visual Basic developers wrote business applications.
C# brings the rapid development paradigm of VB to the world of C++ developers, w
ith some obvious changes. C# takes advantage of the .NET Framework, which means
you have access to a powerful forms engine, just like VB developers have had for
years. New data types have been added, such as the decimal data type for perfor
ming financial calculations.
Type-safety
____________
C# is type-safe, which means several things. For example, you cannot use uniniti
alized variables. In C++ it is easy to declare a variable and then check its val
ue; whatever was in the memory address given to that variable would then be show
n, and this could wreak havoc on an application. The C# complier will notify you
if you try to use a variable before you have initialized it to some valid value
.
With C#, you can no longer just walk past the end of an array, as you have been
able to do in C and C++ for ages. In C++ you could declare an array of three ele
ments and then happily examine the fourth element of that array and get the next
chunk of memory.
Object-oriented
_______________
While many would argue that C++ is object-oriented, C# goes to another level. Ev
en simple data types can be treated as objects, meaning that an int has methods
associated with it. For example, you can use the ToString method to get a string
value for an int, as shown below.
int Counter=14;
Console.Write(Counter.ToString());

In addition, literal strings can be treated as objects and support a variety of


methods, such as Trim, ToUpper, ToLower, and many others, as shown here:
Console.Write("hello, world".ToUpper());

Simplified syntax
__________________
While C++ is an extremely powerful language, it has not typically been considere
d easy. C# attempts to simplify the syntax to be more consistent and more logica
l while also removing some of the more complex features of C++. For example, C#
does away with pointers. As a type-safe language, C# doesn't allow direct memory
manipulation, so pointers are no longer needed in C#.
Header files have also been removed from C#. The namespace and reference operato
rs, :: and -> respectively, have been replaced with a single operator, the perio
d (.).
Perhaps one of the biggest changes is that the int and bool data types are now c
ompletely different. This means that you will finally have an end to the assignm
ent vs. comparison problem in if statements. In other words, the following code
will not even compile under C#:
int Counter=14;
if (Counter=14) { //do something }

Attempting to compile this code will return an error stating:


Cannot implicitly convert type int to bool

C# also removes memory management issues from the developer by using .NET s garbag
e collection scheme. Items no longer referenced are marked for garbage collectio
n, and the Framework can reclaim this memory as needed.
XML comments
_____________
C# supports the introduction of XML comments. Far from being just another way to
add comments to code, XML comments can actually turn into your documentation. T
he comments are placed into XML format and can then be used as needed to documen
t your code. This documentation can include example code, parameters, and refere
nces to other topics. It finally makes sense for a developer to document his or
her code, because those comments can actually become documentation independent o
f the source code.
Not just Microsoft
___________________
C# is not just about Microsoft anymore. Microsoft released C# to ECMA (take that
, Java programmers!) and it has been published as a standard. In addition, the M
ono project is an effort to make an open source version of the .NET Framework (a
subset of it called the CLI) and an open source version of C#, all for Linux. Y
ou can read more about Mono and download it here.
All about components
____________________
Creating components is straightforward, as is referencing those components in co
de. Namespaces in C# replace a lot of the headaches from the COM world in which
you had to perform registry lookups and instantiate objects and worry about such
things as IUnknown and IDispatch. Using C#, you simply import a namespace and t
hen begin using the classes in that component no registry lookups or COM plumbing
required.
The power to be unsafe
______________________
Much is made about C# s type-safety, but if you want to drive without a seatbelt,
you are free to do so. If you really need to use pointers, for example, you can
use the unsafe keyword to mark a block of code. This allows that code to bypass
the Framework s type-safety checking and means that you can directly manipulate me
mory. This makes C# incredibly powerful and is one of the advantages of C# over
VB.NET.
Cross-language capabilities
___________________________
C# has the ability to allow you to interoperate with any other language on the .
NET platform. Much has been said about how you can create a component in one lan
guage and inherit and extend that component in another language, which is someth
ing that was difficult, if not impossible, with COM.
C# also supports the concept of error handling across different languages. Gone
are the unfriendly HResults; instead, you have access to .NET exceptions, which
are consistent across any .NET language.
It might be worth more
_______________________
When it comes to creating applications for .NET, the choice between VB.NET and C
# is typically not important. However, it is quite possible that companies are w
illing to pay more for C# developers; C++ developers have typically been harder
to find than VB developers and have been better compensated. It's likely there w
ill be more VB.NET developers than C# developers, so this trend may well continu
e into the future.

Potrebbero piacerti anche