C sharp Programming Language Introduction-Modern Programing Language-Lecture Handout, Exercises of Programming Languages

Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Java, Commonalities, Programming, Language, Introduction, Microsoft, Intermediate, Features, Reference

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C# Programming Language(Lecture 31-34) VU
C# Programming Language(Lecture 31-34)
An Introduction
C# was released by Microsoft in June 2000 as part of the .Net framework. It was co-
authored by Anders Hejlsberg (who is famous for the design of the Delphi language), and
Scott Wiltamuth. C# is a strongly-typed object-oriented language. It is Similar to Java and
C++ in many respects. The .NET platform is centered around a Common Language
Runtime (CLR - which is similar to a JVM) and a set of libraries which can be exploited
by a wide variety of languages which are able to work together by all compiling to an
intermediate language (IL).
Java and C# - Some Commonalities
Java and C# are very similar and have a number of commonalities. Both of these
languages compile into machine-independent language-independent code which runs in a
managed execution environment. Both C# and Java compile initially to an intermediate
language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode. In
each case the intermediate language can be run - by interpretation or just-in-time
compilation - on an appropriate 'virtual machine'. In C#, however, more support is given
for the further compilation of the intermediate language code into native code.
Both of these have garbage Collection coupled with the elimination of pointers (in C#
restricted use is permitted within code marked unsafe). Both have powerful reflection
capabilities. In case of both these languages, there is no header files, all code scoped to
packages or assemblies, no problems declaring one class before another with circular
dependencies. Both of these support only OOP and classes all descend from object and
must be allocated on the heap with new keyword. Both support concurrency through
thread support by putting a lock on objects when entering code marked as
locked/synchronized.
Both have single inheritance and support for interfaces. There are no global functions or
constants, everything belongs to a class. Arrays and strings with built-in bounds checking.
The "." operator is always used and there are no more ->, :: operators. null and
boolean/bool are keywords. All values must be initialized before use. Integers cannot be
used to govern if statements and conditions in the loops. In both languages try Blocks can
have a finally clause.
Some C# features which are different from Java
C# has more primitive data types than Java and has more extension to the value types. It
supports safer enumeration types whereas Java does not have enumeration types. It also
has support for struct which are light weight objects. There is support for operator
overloading. C# has the concept of 'delegates' which are type-safe method pointers and
are used to implement event-handling. It supports three types of arrays: single
dimensional, multi-dimensional rectangular, and multi-dimensional jagged arrays.
It has restricted use of pointers. The 'switch' statements in C# has been changed so that
'fall-through' behaviour is disallowed. It also has support for class 'properties'.
C# Hello World
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download C sharp Programming Language Introduction-Modern Programing Language-Lecture Handout and more Exercises Programming Languages in PDF only on Docsity!

C# Programming Language(Lecture 31-34)

An Introduction C# was released by Microsoft in June 2000 as part of the .Net framework. It was co- authored by Anders Hejlsberg (who is famous for the design of the Delphi language), and Scott Wiltamuth. C# is a strongly-typed object-oriented language. It is Similar to Java and C++ in many respects. The .NET platform is centered around a Common Language Runtime (CLR - which is similar to a JVM) and a set of libraries which can be exploited by a wide variety of languages which are able to work together by all compiling to an intermediate language (IL).

Java and C# - Some Commonalities

Java and C# are very similar and have a number of commonalities. Both of these languages compile into machine-independent language-independent code which runs in a managed execution environment. Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode. In each case the intermediate language can be run - by interpretation or just-in-time compilation - on an appropriate 'virtual machine'. In C#, however, more support is given for the further compilation of the intermediate language code into native code.

Both of these have garbage Collection coupled with the elimination of pointers (in C# restricted use is permitted within code marked unsafe). Both have powerful reflection capabilities. In case of both these languages, there is no header files, all code scoped to packages or assemblies, no problems declaring one class before another with circular dependencies. Both of these support only OOP and classes all descend from object and must be allocated on the heap with new keyword. Both support concurrency through thread support by putting a lock on objects when entering code marked as locked/synchronized.

Both have single inheritance and support for interfaces. There are no global functions or constants, everything belongs to a class. Arrays and strings with built-in bounds checking. The "." operator is always used and there are no more ->, :: operators. null and boolean/bool are keywords. All values must be initialized before use. Integers cannot be used to govern if statements and conditions in the loops. In both languages try Blocks can have a finally clause.

Some C# features which are different from Java

C# has more primitive data types than Java and has more extension to the value types. It supports safer enumeration types whereas Java does not have enumeration types. It also has support for struct which are light weight objects. There is support for operator overloading. C# has the concept of 'delegates' which are type-safe method pointers and are used to implement event-handling. It supports three types of arrays: single dimensional, multi-dimensional rectangular, and multi-dimensional jagged arrays.

It has restricted use of pointers. The 'switch' statements in C# has been changed so that 'fall-through' behaviour is disallowed. It also has support for class 'properties'.

C# Hello World

Let us have a look at the Hello World Program in C#.

using System; // System namespace public class HelloWorld { public static void Main() // the Main function starts // with capital M { Console.WriteLine("Hello World!”); } }

In this example, it may be noted that just like Java and C++, C# is case sensitive. As we have in Java, everything in C# has to be inside a class and there is no semicolon at the end of the class. However, unlike Java, name of the class and the name of the file in which it is saved do not need to match up. You are free to choose any extension for the file, but it is usual to use the extension '.cs'. It supports single line and multiple line comments.

Variable Types: Reference Types and Value Types

As mentioned earlier, C# is a type-safe language. Variables can hold either value types or reference types, or they can be pointers. When a variable v contains a value type, it directly contains an object with some value and when it contains a reference type, what it directly contains is something which refers to an object.

Built-in Types

Structs

Java did not have structs which have been braought back by C#. However, as compared to C++, they are significantly different in C#. In C++ a struct is exactly like a class, except that the default inheritance and default access are public rather than private. In C# structs are very different from classes. Structs in C# are designed to encapsulate lightweight objects. They are value types (not reference types), so they're passed by value. They are sealed, which means they cannot be derived from or have any base class other than System.ValueType, which is derived from Object. Structs cannot declare a default (parameterless) constructor.

Properties

C# has formalized the concept of getter/setter methods. The relationship between a get and set method is inherent in C#, while has to be maintained in Java or C++. For example, in Java/C++ we will have to write code similar to the one shown below:

public int getSize() { return size; }

public void setSize (int value) { size = value; }

foo.setSize (getSize () + 1);

In C# we can define a property and can use it as if we were using a public variable. This is shown below:

public int Size { get {return size; } set {size = value; } }

foo.size = foo.size + 1;

Reference types

In C#, the pre-defined reference types are object and string. As mentioned earlier, object is the ultimate base class of all other types. New reference types can be defined using 'class', 'interface', and 'delegate' declarations. Reference types actually hold the value of a memory address occupied by the object they reference.

Reference types however suffer from the problem of aliasing as shown below:

object x = new object(); x.myValue = 10; object y = x; y.myValue = 20; // after this statement both x.myValue and y.myValue // equal 20

There is however no aliasing in string. That is, strings are immutable. The properties of an immutable object can't be modified. So in order to change what a string variable references, a new string object must be created. Following is an example that elaborates this concept:

string s1 = "hello"; string s2 = s1; // s2 points to the same strings as s s1 = “world”; // a new string is created and s1 points to it. // s2 keeps pointing to the old strings

Pointers

Pointers were present in C++ but Java designers took them out. C# has brought them back. However, C#, pointers can only be declared to hold the memory addresses of value types. That is, we cannot have pointers for reference types. The rest is very similar to C++. This is illustrated with the help of the following example:

int i = 5; int *p; p = &i; // take address of i *p = 10; // changes the value of i to 10

One major difference between C++ and C# is that the „*‟ applies to the type. That is, as opposed to C++, in C#, the following statement would declare two pointers p1 and p2:

int * p1, p2;

Just like C++, the dereference operator „->‟ is used to access elements of a struct type.

Pointers and unsafe code

In C#, we have two modes for the code, the managed and unmanaged. Theses are elaborated as below: