View
213
Download
0
Embed Size (px)
Citation preview
C#
A Semitone Higher
History
We first begin with Java which was released in 1995 by Sun Microsystems
Initially Java was 100% interpreted at runtime and was very slow
Eventually Just-in-Time compilers were created and used
The increased performance of Java and portability helped it grow in popularity
History
The theory that Java applications could be developed on Windows and then easily be deployed on Unix platforms was clearly a threat to Microsoft
So Microsoft created their own Java Virtual Machine which was fairly reliable
However, their JVM introduced incompatible extensions which ruined portability
History
So Sun sued Microsoft for violating the licensing terms
This hindered Microsoft’s JVM making it obsolete quickly as Sun updated their JVM
Clearly Microsoft and Java did not mix well
History
In 1999 Anders Hejlsberg of Microsoft and his team began working on a new language initially called COOL (C-like Object Oriented Language)
The name was eventually changed to C# by the time it was announced, along with Microsoft’s .NET, in 2000
History
James Gosling claimed that C# was an “imitation” of Java“[C# is] sort of Java with reliability,
productivity and security deleted.” Klaus Kreft and Angelika Langer
(authors)“Java and C# are almost identical
programming languages. Boring repetition that lacks innovation.”
History
However, over time Java and C# have taken different paths
Overview Part of the .NET
Framework Compiler
creates intermediate code (CIL)
CLR creates machine code
Just-in-time compilation
.exe or .dll
C# code can be compiled either to executable files or to library files (dynamically linked library)
csc program.cs compile to executable
csc /t:library lib.cs compile to dll
C like language
For the most part, if you have programmed in Java, C, C++ or any other C like language, you will be used to most C# syntax
C# uses {..} block statements If else, while, do/while, for statements all
the same Even many keywords are the same
(especially compared to Java)
Object Oriented
At the heart it is object oriented
Supports inheritance and polymorphism
Classes are like objects with membersmethods, constructors, etc.
Basic Program
The Main methodstatic void Main() {…}static void Main(string[] args) {….}static int Main() {….}static int Main(string[] args) {…}
“Other overloaded versions of Main are permitted, however, provided they have more than one parameter, or their only parameter is other than type string[].” - Microsoft
Identifiers and Keywords
C# has 80 keywords Some are context sensitive keywords
They can be used as identifiers All keywords are usable as identifiers if
the @ symbol is in front of them@return, @null, @doubleint @int = 5; Console.WriteLine(@int);
Formatted Output
For output:Console.WriteLine(…..);
Format using {…} within a string{parameter #, spacing : special formatting}
Console.WriteLine(“{0,-10}.”, 100);> 100.
...WriteLine(“{1}, {0}”, firstName,lastName);>Rahimi, Shahram
Basic Programs
Basic Programs
Types
Value Typessbyte, short, int, long, byte (unsigned),
ushort, uint, ulong, char, float, double, decimal, bool
Enum, Struct, Nullable
Reference TypesObjects, string, class, interface, array,
delegate
Arrays
Arrays are treated the same as in Javaint[] n = new int[]{1,2,3,4};
SAME ASint[] n = {1,2,3,4};
SAME ASint[] n = new int[4];
n[0] = 1;
…..
Types
Nullable typeint? allows the int values to also be nullUseful for databases
DelegatesA data structure that refers to one or more
methodsSimilar to function pointers in C and C++
Delegates
Delegates
Switch
Switch statements are mostly the same EXECPT that they require an explicit branch statement like break or goto due to a static semantic rule
Also, switch statements in C# allow strings along with int and char
Switch
Unsafe Code
While C# has made strides to eliminate the need for pointers as data types with references and objects, it is still allowed
One must declare unsafe code to:Declare and operate on pointersPerform conversions between pointersTake addresses of variables
Unsafe Code Variables
Methods
Classes
Readability Pro
Basic syntax is C like (recognizable)Data must be explicitly typed and declaredVery common special words and statement
structure (loops and selection) Con
Not necessarily simple (delegates)Overloaded MainKeywords as identifiersAll statements end with }
Writability
ProMany ways to do one thing (like array
declaration)Delegates can simplify method callingInheritance
Con80 keywords to remember (and required
context)
Reliability
ProLimits use of pointers, programmer
becomes very aware of possible pointer issues.
Uses explicitly typed and declared variables
ConCan write code in unsafe mode Can be a complex language
CostMemory references and automatic garbage
collection make creating quality code simpler and faster
Uses Microsoft Visual Studio as compiler ○ Free open source versions are available, but
premium versions can cost $2,000+Similar to well known languages but can be
a complex language
Who is using C#
It’s Microsoft Who isn’t using it?
Web design Gaming Medical Financial
Mono ProjectUNIX version of the Microsoft .NET
development platform
Open sourced based on C# .NET frameworkEnables Multi platform UNIX .NET
applications Implements various technologies developed
by Microsoft that have now been submitted to the ECMA for standardization.
Sources “C# In Depth” – Jon Skeet Wikipedia
http://en.wikipedia.org/wiki/C_Sharp_(programming_language)
“C sharp Language Specification” – Microsoft
“Concepts of Programming Languages” -Sebesta
“Essential C# 4.0” – Michaelis http://www.mono-project.com/