Programming in C#: CSE 459.24 Prof. Roger Crawfis
Programming in C#: CSE 459.24 Prof. Roger Crawfis
Programming in C#: CSE 459.24 Prof. Roger Crawfis
Course Overview
1-credit pass/no-pass brief introduction to C#. Covers (most of) the C# language and some of the most useful .NET APIs. Should not be your first programming class.
Assume you know C++ and/or Java and basic object-oriented or component-based programming. C# and .NET cannot be learned thoroughly in this brief course.
S/U Details
Syllabus
Background, history, CLI, CIL, CLR, CTS, C# Types
Primitive types, Classes, Properties, Interfaces, Delegates, Events, Generic types. foreach, yield, events, is/as (type casting), lock.
C# language features
Common Interfaces
Programming in C#
C# History
CSE 459.24 Prof. Roger Crawfis
History of C#
Developed by Microsoft. Based on Java and C++, but has many additional extensions. Java and C# are both being updated to keep up with each other. Cross-development with Visual Basic, Visual C++, and many other .NET languages.
Classification of C#
Wikipedia.org definition.
Object-oriented. Primarily imperative or procedural.
Structured (as opposed to monolithic). Strongly typed. ISO and ECMA standardized.
.NET History
Visual C# http://www.microsoft.com/express/2008/
in MSDNAA
Mono: http://www.go-mono.com
Open Source for Linux: not quite at 2.0 Shared Source for Windows (through 2.0) Use to work on BSD / OS X, too yet another open source implementation
Rotor: http://msdn.microsoft.com/net/sscli
Portable.NET: http://www.dotgnu.org
Programming in C#
C#, like Java, is executed indirectly through an abstract computer architecture called the CLR.
http://msdn2.microsoft.com/en-us/library/z1zx9t92(VS.80).aspx
The CLR transforms the CIL to assembly instructions for a particular hardware architecture.
This is termed jiting or Just-in-time compiling. Some initial performance cost, but the jitted code is cached for further execution. The CLR can target the specific architecture in which the code is executing, so some performance gains are possible.
CLR is actually an implementation by Microsoft of the CLI (Common Language Infrastructure) . CLI is an open specification. CLR is really a platform specific implementation.
from wikipedia.org
Common Type System (CTS) Meta-data in a language agnostic fashion. Common Language Specification behaviors that all languages need to follow. A Virtual Execution System (VES).
A specification for how types are defined and how they behave.
CTS also specifies the rules for visibility and access to members of a type:
Other rules
Object life-time Inheritance Equality (through System.Object)
CTS defines System.Int32 4 byte integer C# defines int as an alias of System.Int32 C# aliases System.String as string.
From MSDN
how methods may be called when constructors are called subset of the types in CTS which are allowed
Code that takes UInt32 in a public method UInt32 is not in the CLS not marked is assumed to mean not compliant
For example
Built-in Types
C#
int uint sbyte byte short ushort long ulong float double decimal char string object
CLS compliant
yes no no yes yes no yes no yes yes yes yes yes yes
Blittable types
Most of these types are blittable, meaning their memory layout is consistent across languages and hence, support interoperability. The types bool, char, object and string are not and must be Marshaled when using these between languages. Single dimensional arrays of blittable types are also blittable.
Programming in C#
Assemblies
CSE 494R
(proposed course for 459 Programming in C#)
Assemblies
code and metadata .exe or .dll as before Executable needs a class with a Main method:
types
local: local assembly, not accessible by others shared: well-known location, can be GAC strong names: use crypto for signatures
PE executable file
Structure of PE file PE header Entry point address MS IL instructions Other initial settings native instructions e.g., x86 instructions
First C# Program
using System; namespace Test
{
Constructions of Note
using
namespace
disambiguation of names like Internet hierarchical names and C++ naming
class
like in C++ or Java single inheritance up to object
Constructions of Note
Defines the entry point for an assembly. Four different overloads taking string arguments and returning ints. Takes a formatted string: Composite Format Indexed elements: e.g., {0}
Console.Write(Line)
{index [,alignment][:formatting]}