C# - Programming Languages Compare-Off

Learn about Visual C# .NET 2003

Similar language:

Java.


Name binding:


Done at compile time, but the programmer can use reflection/late-binding to do binding at run time whick is  useful when creating a custom script to be run by the user.


Scope rules:

Class scope - From when created in class until the end of class, global to all methods in that class (for direct modification), and repeated names cases previous to be hidden until scope ends.

Block scope - From when created until the end of the block, and is only used within that block, and must be passed and modified indirectly.
There is a restriction such that local variables can only be given names that allow them to be uniquely identified in a given scope.  If a name has more than one scope and there is no way to disambiguate the name, the innermost declaration of the name is an error and must be changed.


Type system:

Basic types -  byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, string, char, bool.

Type constructors - The struct keyword allows a programmer to extend the type system and implement a custom value type.  Structs are stack allocated, can have member functions defined on them, and are boxed and unboxed as necessary.

Strongly typed, statically typed, explicit typing done by using the case operator.


Memory management:

Dynamic memory allocation provides automatic memory management for the programmer, but pointer declarations and the stackalloc command allow the programmer to allocate memory manually.  There are manual disposal methods, and automatic garbage collection.


Call mechanisms:

By-value and by-reference.


Eval strategy:

Strict/eager evaluation.


Refs/pointers as first-class types:

Yes.


Higher-order functions:

Yes.


Object/Class abstraction:

Since C# is an OO language, inheritance is a fundamental feature of the language.  However, C# and the .NET Runtime do not support multiple inheritance for classes, but do support multiple implementation of interfaces.


Nondeterminism:

Yes, overloading available.


Polymorphism (templates, generics, functors):

Abstract functions are automatically virtual functions, which allow the programmer to use polymorphism to make their code simpler.  The compiler will write code to call the appropriate version of the function at runtime.


Concurrency (processes, coroutines, threads, fork/join):

Microsoft .NET and the common language runtime (CLR) expose many of the rich threading features that can be implemented in C#.


Compiled vs. interpreted vs. mixed:

C# is a compiled language.


Main intended use or domain of application:

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.


Support for or use of assertions/invariants/pre-post-conditions/contract:

In the System.Diagnostics namespace there are Debug and Trace classes that allow a programmer to implement asserts.  An assert is simply a statement of a condition that should be true, followed by some text to output if it is false.

Example code:

A minimal C# program is implemented like this:

using System;
class Test {
   static void Main(  ) {
      System.Console.WriteLine("Welcome to C#!");
   }

}



A C# program for the Fibonacci series:

class FS 
{ 
            public static void Main() 
            { 

                        int n; 
                        Console.WriteLine("Number of terms to be generated ?"); 
                        string s=Console.ReadLine(); 
                        n=Int32.Parse(s); 
                        Console.WriteLine("*******************************"); 

                        Console.WriteLine("Fibonacci seqence upto {0}",n); 
                        Console.WriteLine("*******************************"); 
                        test1.generateFib(n); 
            } 
} 

  
class generateFib 
{ 
            static int f1=0; 
            static int f2=1; 
            public static void fib(int n) 
            { 

                        int temp; 
                        if(n<2) 
                        { 
                                    f1=0; 
                                    f2=1; 
                        } 

                        else 
                        { 
                                    fib(n-1); 
                                    temp=f2; 
                                    f2=f1+f2; 
                                    f1=temp; 

                        } 
            Console.WriteLine(f1); 
            } 
}