Programming Languages
A Dave Hillman Project

[Home] [Contact Me] [Site Map] [Search] [References]

C#

 

[Up]

[Language Basics]
[C Language]
[C++]
[C#]
[Java]
[JavaScript]
[Visual Basic 6.0]
[VBScript]
[VB.Net]
[.Net Technology]

 

 

Introduction

C# (pronounced C-sharp) is part of the Microsoft .Net architecture.  Many people argue that C# is more of a competitor to Java (and specifically to Sun Microsystems) than to C or C++.  Officially Microsoft states that it is an evolution of C/C++.

Essentially C# is a purely object oriented development environment that incorporates a garbage collection mechanism to clear memory of no longer used data.

C# is an object-oriented language supporting:

bulletNamespaces - allow developers to group related types into hierarchical categories.
bulletInheritance - from one class to others (multiple inheritance to a class is not allowed); new classes can be customized.
bulletPolymorphism - the ability to perform the same operation across a number of types (as long as they share common characteristics).

C# applications programming depends upon the .Net Framework which provides a robust set of services and libraries.  This includes both the framework class library (FCL) and the Common Language Runtime (CLS).

bulletThe Framework Class Library (FCL) includes the following libraries:
bulletObject Class - root of the class hierarchy and serves as base class for all others.
bulletMath - including special data types (decimal for money), math functions (trig, etc.), and random number generation.
bulletString - formatting, indexing, and encoding
bulletCollections - data arrays to supplement standard arrays.
bulletRegular Expressions - expression matching and replacement.
bulletInput/Output
bulletNetworking

Language Fundamentals

Program Structure and Organization

bulletA C# program is built by building new types and using existing ones created in C# language or imported from other libraries. 
bulletC# programs can consist of one or more files.
bulletEach file can contain one or more namespaces.
bulletA namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces.
bulletThe following is the skeleton of a C# program that contains all of these elements.

 

// A skeleton of a C# program
using System;
namespace MyNamespace1
{
   class MyClass1
   {
   }
   struct MyStruct
   {
   }
   interface IMyInterface
   {
   }
   delegate int MyDelegate();
   enum MyEnum
   {
   }
   namespace MyNamespace2
   {
   }
   class MyClass2
   {
      public static void Main(string[] args)
      {
      }
   }
}

--from Microsoft C# Reference site

 

bulletCommenting code:
bullet// comment

/* multiline
 comment */
bulletProgram delivery: compiled
bulletPrimarily via tools by Microsoft
bullet http://www.c-point.com/csharp.htm also has a C# development product.
bulletKey words in C# 

 

abstract event new struct
as explicit null switch
base extern object this
bool false operator throw
break finally out true
byte fixed override try
case float params typeof
catch for private uint
char foreach protected ulong
checked goto public unchecked
class if readonly unsafe
const implicit ref ushort
continue in return using
decimal int sbyte virtual
default interface sealed volatile
delegate internal short void
do is sizeof while
double lock stackalloc
else long static
enum namespace string


 

Data Types

Storage size  
Decimal decimal
Date DataTime (.NET Framework class)
(varies) String
1 byte Byte
2 bytes Boolean - bool
2 bytes short, char (Unicode character)
4 bytes int
8 bytes long
4 bytes float
8 bytes double

 

bulletArrays - C# supports arrays (zero referent).  It also support multi-dimensional arrays as both rectangular (n-dimensional blocks) and jagged (arrays of arrays).

Variables

bulletThe C# typing system contains the following categories:
bulletValue types -- the value types consist of two main categories:
  1. Struct type (integral, floating point, decimal, bool)
  2. Enumeration type: used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list
bulletReference types - also referred to as objects, store references to the actual data; 3 keywords are used to declare reference types:
bulletclass
bulletinterface -- defines a contract. A class or struct that implements an interface must adhere to its contract.
bulletdelegate -- defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
bulletPointer types
bulletVariables of the value types store data, while those of the reference types store references to the actual data. Reference types are also referred to as objects. Pointer types can be used only in unsafe mode.
bulletIt is possible to convert a value type to a reference type and vice versa, by using boxing and unboxing.

 

Expressions and Operators

  Addition +
  Subtraction -
  Multiplication *
  Division /
  Integer division /1
  Modulus (division returning only the remainder) %
  Exponentiation n/a

Assignment

 
  Assignment =
  Addition +=
  Subtraction -=
  Multiplication *=
  Division /=
  Integer division /=1
  Concatenate +=
  Modulus %=
  Left shift <<=
  Right shift >>=
  Bitwise-AND &=
  Bitwise-exclusive-OR ^=
  Bitwise-inclusive-OR |=

Relational/equality

 
  Less than <
  Less than or equal to <=
  Greater than >
  Greater than or equal to >=
  Equal ==
  Not equal !=
  Compare two object reference variables ==
  Compare object reference type x is Class1 (also see as and typeof)
  Compare strings ==

-or-String.Equals()

  Concatenate strings +
  Shortcircuited Boolean AND &&
  Shortcircuited Boolean OR ||

Shift

 
  Left shift <<
  Right shift >>

Scope resolution

 
  Scope resolution . and base

Postfix (highest order of resolution)

 
  Array element [ ]
  Function call ( )
  Type cast (type)
  Member selection .
  Postfix increment ++
  Postfix decrement --

Unary

 
  Indirection * (unsafe mode only)
  Address of & (unsafe mode only; also see fixed)
  Logical-NOT !
  One's complement ~
  Prefix increment ++
  Prefix decrement --
  Size of type sizeof
  comma n/a

Bitwise

 
  Bitwise-AND &
  Bitwise-exclusive-OR ^
  Bitwise-inclusive-OR |

Logical

 
  Logical-AND &&
  Logical-OR ||

Conditional

 
  Conditional ?:

Pointer to member

 
  Pointer to member . (Unsafe mode only)

Reference

 
  Reference n/a (use reference types)

 

Control Statements

bulletIf-then

The if statement selects a statement for execution based on the value of a Boolean expression. It takes the following form:

if (expression)
   statement1
[else
   statement2]

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators.
statement1
The embedded statement(s) to be executed if expression is true.
statement2
The embedded statement(s) to be executed if expression is false.

 

bulletSwitch

The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. It takes the following form:

switch (expression)
{
   case constant-expression:
      statement
      jump-statement
   [default:
      statement
      jump-statement]
}

Where:

expression
An integral or string type expression.
statement
The embedded statement(s) to be executed if control is transferred to the case or the default.
jump-statement
A jump statement that transfers control out of the case body.
constant-expression
Control is transferred to a specific case according to the value of this expression.
bulletDo loop

The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:

do statement while (expression);

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria.
statement
The embedded statement(s) to be executed.
bulletFor loop

The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:

for ([initializers]; [expression]; [iterators]) statement

where:

initializers
A comma separated list of expressions or assignment statements to initialize the loop counters.
expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria.
iterators
Expression statement(s) to increment or decrement the loop counters.
statement
The embedded statement(s) to executed.

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects. The statement takes the following form:

 

bulletWhile

The while statement executes a statement or a block of statements until a specified expression evaluates to false. It takes the following form:

while (expression) statement

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria.
statement
The embedded statement(s) to be executed.
bulletJump statements in C#:
bulletBreak - exit from a loop.
bulletContinue - skips remaining steps, starts on next iteration of a loop.
bulletGoTo - transfers control to a label.
bulletReturn - exits a method.
bulletThrow - "throws" an exception to indicate that an abnormal condition has occurred.
bulletLock - calls 'enter' and 'exit' methods for the monitor class.
bulletUsing - enables declaring, using, and disposing of disposable objects (e.g., file handling).

 

Input/Output

bulletCommand Line: via the console object interfaces.
bulletGUI: via the Visual Studio .Net development environments.  This includes use of windows and standard GUI objects (text boxes, buttons, etc.).
bulletFile I/O: via low-level and system level objects.
bulletDatabase: via ADO, ADO.Net and similar database integration technologies.

Resources

References

bulletGoogle Directory: http://directory.google.com/Top/Computers/Programming/Languages/C-sharp/
bullet Microsoft MSDN Reference
bullet Developersdex C# references

Tutorials

bulletC# Station

 

 

 

[Home] [Language Basics] [C Language] [C++] [C#] [Java] [JavaScript] [Visual Basic 6.0] [VBScript] [VB.Net] [.Net Technology]

Copyright © 2003-2005 by Dave Hillman