|
| |
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:
 | Namespaces - allow developers to group related types into hierarchical
categories. |
 | Inheritance - from one class to others (multiple inheritance to a class is
not allowed); new classes can be customized. |
 | Polymorphism - 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).
 | The Framework Class Library (FCL) includes the following libraries:
 | Object Class - root of the class hierarchy and serves as base class for
all others. |
 | Math - including special data types (decimal for money), math functions
(trig, etc.), and random number generation. |
 | String - formatting, indexing, and encoding |
 | Collections - data arrays to supplement standard arrays. |
 | Regular Expressions - expression matching and replacement. |
 | Input/Output |
 | Networking |
|
Language Fundamentals
Program Structure and Organization
 | A C# program is built by building new types and using existing ones
created in C# language or imported from other libraries. |
 | C# programs can consist of one or more files.
 | Each file can contain one or more namespaces. |
 | A namespace can contain types such as classes, structs, interfaces,
enumerations, and delegates, in addition to other namespaces. |
|
 | The 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
 | Commenting code:
 | // comment
/* multiline
comment */ |
|
 | Program delivery: compiled
|
 | Key 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 |
 | Arrays - C# supports arrays (zero referent). It also support
multi-dimensional arrays as both rectangular (n-dimensional blocks) and jagged
(arrays of arrays). |
Variables
 | The C# typing system contains the following categories:
 | Value types -- the value types consist of two main categories:
- Struct type (integral, floating point, decimal, bool)
- Enumeration type: used to declare an enumeration, a distinct
type consisting of a set of named constants called the enumerator list
|
 | Reference types - also referred to as objects, store references
to the actual data; 3 keywords are used to declare reference types:
 | class |
 | interface -- defines a contract. A class or struct that
implements an interface must adhere to its contract. |
 | delegate -- 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. |
|
 | Pointer types |
|
 | Variables 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. |
 | It 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
 | If-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.
 | Switch |
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.
 | Do 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.
 | For 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:
 | While |
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.
 | Jump statements in C#:
 | Break - exit from a loop. |
 | Continue - skips remaining steps, starts on next iteration of a loop. |
 | GoTo - transfers control to a label. |
 | Return - exits a method. |
 | Throw - "throws" an exception to indicate that an abnormal condition has
occurred. |
 | Lock - calls 'enter' and 'exit' methods for the monitor class. |
 | Using - enables declaring, using, and disposing of disposable objects
(e.g., file handling). |
|

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

Resources
References
Tutorials
|