Concepts of Programming Languages

(Sean Pound) #1
convert values between the two systems—for example, to put a primitive value
into a collection that stores objects. C# makes the conversion between values
of the two typing systems partially implicit through the implicit boxing and
unboxing operations, which are discussed in detail in Chapter 12.^20
Among the other features of C# are rectangular arrays, which are not sup-
ported in most programming languages, and a foreach statement, which is an
iterator for arrays and collection objects. A similar foreach statement is found
in Perl, PHP, and Java 5.0. Also, C# includes properties, which are an alterna-
tive to public data members. Properties are specified as data members with get
and set methods, which are implicitly called when references and assignments
are made to the associated data members.
C# has evolved continuously and quickly from its initial release in 2002.
The most recent version is C# 2010. C# 2010 adds a form of dynamic typing,
implicit typing, and anonymous types (see Chapter 6).

2.19.3 Evaluation


C# was meant to be an improvement over both C++ and Java as a general-
purpose programming language. Although it can be argued that some of its
features are a step backward, C# clearly includes some constructs that move
it beyond its predecessors. Some of its features will surely be adopted by pro-
gramming languages of the near future. Some already do.
The following is an example of a C# program:

// C# Example Program
// Input: An integer, listlen, where listlen is less than
// 100, followed by listlen-integer values.
// Output: The number of input values that are greater
// than the average of all input values.
using System;
public class Ch2example {
static void Main() {
int[] intlist;
int listlen,
counter,
sum = 0,
average,
result = 0;
intList = new int[99];
listlen = Int32.Parse(Console.readLine());
if ((listlen > 0) && (listlen < 100)) {
// Read input into an array and compute the sum
for (counter = 0; counter < listlen; counter++) {


  1. This feature was added to Java in Java 5.0.


2.19 The Flagship .NET Language: C# 103
Free download pdf