TIE-23100 Principles of Programming Languages Introduction to C# Language Group Number: 45 - ArvinJalali 227226 Baptiste Geslin 224661

Page created by Ryan Alvarez
 
CONTINUE READING
TIE-23100 Principles of Programming Languages
Introduction to C# Language
Group Number: 45
Arvin Jalali   227226

Baptiste Geslin 224661
Abstraction: C# is a multi paradigm programming language developed by Microsoft initially
within .NET, and later approved as a standard by ISO and Ecma. C# is designed for Common
Language Infrastructure, and it was supposed to be simple, modern, general-purpose and
object-oriented. In this essay, firstly, we discuss about some principles like as design goals
behind C#, and syntax. Then the essay concentrates on distinct features of C#, and tries to
compare it with other languages if possible. Secondly, categorization of data types is discussed.
Furthermore, we emphasize on most important feature of version 2.0 which is generics. Finally
the program “Hello World” as the first program of C# is represented.

Introduction: The name "C sharp" has kind of association with musical notation where a sharp
means that the written note should be a semitone higher in pitch. C# was developed by
Microsoft, and Anders Hejlsberg is the leader of C# development team. In January 1999, Anders
Hejlsberg performed a team in order to create a new language called Cool, which was kind of C-
like Object Oriented Language. Then, Microsoft changed the name to C#, and the class libraries
and ASP.NET runtime was ported to it. In general, C# is supposed to be simple, object-oriented,
and general-purpose. The most recent version of C# is C# 5.0 which was released on August
2012. C# is a multi-paradigm programming language including imperative, declarative,
functional, procedural, generic, object-oriented, and component-oriented programming
disciplines.

Design Goals of C#: In design step, indeed, C# is supposed to support key principles like as
strong type checking, array bounds checking, detection of attempts to use uninitialized
variables, and automatic garbage collection. Also, it should be suitable for developing
components in distributed environments. Portability has a great importance, also C# is
supposed to be economical regarding memory and processing speed. In design, C# was
assumed to reflect the basics of Common Language Infrastructure (CLI). The Common Language
Infrastructure (CLI) is an open specification developed by Microsoft and standardized by ISO
and ECMA that describes the executable code and runtime environment that form the core of
the Microsoft .NET Framework. The specification defines an environment which allows multiple
high-level languages to be used on different computer platforms without being rewritten for
specific architectures.

Syntax: In C#, we use semicolons to show the end of each statement. Pair of braces is used to
group a set of statements. As a general rule, statements are basically grouped into functions,
functions into classes, and classes into namespaces. Assignment is done by an equal sign,
however equality is shown by double equal signs. To declare an array, and assigning a value to a
specific index, a pair of brackets is used.

Distinguishing feature of C#: Here, we try to express some important features of C# that makes it
distinct in contrast with other language like as C++ or Java. First of all, C# is a strongly-typed
programming language. It means that C# support implicit variable declaration by using the
keyword “var”, also by using “new[]” for the arrays. C# provides namespaces which acts like as
packages in java in order to perform code isolation. In c#, Boolean data type is quite strict
compared to C++. As we know, in C++, Boolean type can be freely converted to and from
integers, thus, expressions like as if(X) or while(X) needs only that X is convertible to bool, and
C++ allows that X can be from type int, or a pointer. C# by strictly disallowing this feature and
forcing the programmers to use only expression which are from type Boolean prevent some
common mistakes in C++ for example a mistake like as if(a=b). In general, in C# the expression
of if and while should be from type Boolean. In C#, memory management is done automatically
by garbage collection by addressing the memory leaking, so the programmers do not need to
be worried about the responsibility of releasing the memory that is no longer in use. C# does
not support Multiple Inheritance to prevent complexity, and also to make the architectural
requirements simple based on CLI. In C#, types are much safer than C++. That is because the
only by default implicit conversions are those that are supposed to be safe like integers'
conversion to each other. Also, no implicit conversions happen between integers and Booleans.
Any user-defined conversion must be marked as implicit or explicit. C# provides synchronization
method like as java which makes programmers to utilize mutually-exclusive locks via the
keyword lock. We are not allowed to use global variables or functions in C#. All the functions or
variables should be declared within classes. We can use static members of public classes as a
substitution of global variables or functions.

C# data types, value types vs. reference types: In C#, we can categorize data types into two groups
including value types and reference types. Instances of value types do not have referential
identity. Value type variables directly contains their values, it means that the memory is
allocated inline in whatever context the variable is declared. Examples of value types are all
primitive types such as int, float, char, enum (enumerations) and struct (user defined
structures). On the other hand, reference types have referential identity which means that each
instance of a reference type is inherently distinct from every other instance, even if the data
within both instances is the same. As examples, a type that is defined as a class, string or array
is a reference type. When we declare a variable as a reference type, at run time, the variable
contains the value null until we explicitly create an instance of the object by using the new
operator, or assign it an object that has been created elsewhere by using new. When the object
is created, the memory is allocated and the variable holds only a reference to the location of
the object. Boxing means the operation of converting a value-type object into a corresponding
reference type which is implicit in c#, while, Unboxing is the operation of converting a
reference-type object into a value-type one which is explicit in C#.
int foo = 42;                    // Value type.
object bar = foo;                // foo is boxed to bar.
int foo2 = (int)bar;             // Unboxed back to value type.

Generics: Version 2.0 of C# added the generics to language as the most powerful feature of the
version. Generics introduce the meaning of type parameters. By using type parameters, we can
design classes and methods that defer the specification of one or more types until the class or
method is declared and instantiated by client code. As an example of usage of generics, we can
write a single class by using type parameter that can be used by other client code without being
worried about the cost or risk of runtime casts or boxing operations. Indeed, generics make us
able to define type-safe data structures, without committing the actual data types. This can
improve the performance and quality because we can reuse data processing algorithms without
duplicating type-specific code. Conceptually, generics are similar to C++ templates, but they are
strongly different in implementation and capabilities. In more detail, comparing with C++
templates, C# generics can provide enhanced safety, but also have somewhat limitation in case
of capabilities. For example, it is not possible to call arithmetic operators on a C# generic type.

Hello World!  in C#:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Hello world!");
    }
}

using System; This line of code recommends to complier to consider System as a candidate
prefix for types used in the source code. For example, when the complier sees the use of
console type in the source code, first, it tries to find a type named console, after that it fails to
find it, and finally it tried to find a type named System.Console, and it success to find it. In
general, the "using" statement makes programmer to express all candidate prefixes to use
during compilation rather than every time writing the full name. class Program is a class
definition. static void Main() is the class member method. The static keyword makes the
method accessible without an instance of Program. C# compilers report an error, if there is no
static Main method. The void keyword declares that Main has no return value. Finally, the
statement Console.Write("Hello world!"); writes the output. Console is a static class in the
System namespace. It provides an interface to the standard input and output.

References:

http://en.wikipedia.org/wiki/C_Sharp_(programming_language)

http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/ms173104.aspx

http://msdn.microsoft.com/en-us/library/512aeb7t(v=vs.110).aspx
You can also read