Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
Contents
C# Essentials
. O'Reilly, 2001 - S.Robinson et al:
Professional C#
, Wrox Press, 2001
New Features in C# Really new
(compared to Java)
Reference and output parameters
-^
Objects on the stack (structs)
-^
Rectangular arrays
-^
Enumerations
-^
Unified type system
-^
goto
-^
Versioning
Component-based programming-^
Properties
-^
Events
-^
Delegates
-^
Indexers
-^
Operator overloading
-^
foreach statements
-^
Boxing/unboxing
-^
Attributes
-^
Hello World^ File Hello.cs
using System; class Hello {
static void Main() {
Console.WriteLine("Hello World"); } }
uses the namespace
System
entry point must be called
Main
output goes to the console
-^
file name and class name need
not
be identical
Compilation (in the Console window)
csc Hel
lo.cs
Execution
Hel
lo
A Program Consisting of 2 Files Counter.cs
csc Counter.cs Prog.cs=> generates Prog.exe
Prog
csc /target:library Counter.cs=> generates Counter.dllcsc /reference:Counter.dll Prog.cs=> generates Prog.exe
class
Counter
int val = 0; public void
Add
(int x) { val = val + x; }
public int
Val
() { return val; }
} Prog.cs using System; class
Prog
static void
Main
Counter c = new Counter(); c.Add(3); c.Add(5); Console.WriteLine("val = " + c.Val()); } }
Types
Value Types versus Reference Types
value
reference
stack
heap
0, false, '\0'
null
copies the value
copies the reference
int i = 17;
string s = "Hello";
int j = i;
string s1 = s;
i^
17
s
H e l l o
j^
17
s
Simple Types
63
63
64
Enumerations List of named constantsDeclaration (directly in a namespace)
enum Color {red, blue, green}
// values: 0, 1, 2
enum Access {personal=1, group=2, all=4}enum Access1 : byte {personal=1, group=2, all=4}
Color c = Color.blue;
// enumeration constants must be qualified
Access a = Access.personal | Access.group;if ((Access.personal & a) != 0) Console.WriteLine("access granted");
Operations on Enumerations
if (c == Color.red) ...if (c > Color.red && c <= Color.green) ...
c = c + 2;
c++;
if ((c & Color.red) == 0) ...
c = c | Color.blue;
c = ~ Color.red;
( GetName
,^ Format
,^ GetValues
, ...).
Multidimensional Arrays Jagged (like in Java)
a[0][1]
a[0]a[1]
a
int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; int len = a.Length; // 2 len = a[0].Length;
a[0, 1]
int[,] a = new int[2, 3]; int x = a[0, 1]; int len = a.Length;
len = a.GetLength(0); // 2 len = a.GetLength(1); // 3
a
Class System.String Can be used as standard type
string s = "Alfonso";
-^
"Don " + s
-^
s[i]
-^
s.Length
-^
-^
==
!=
if (s == "Alfonso") ...
-^
CompareTo
,^ IndexOf
,^ StartsWith
,^ Substring
, ...
Classes Declaration
class Rectangle {
Point origin;public int width, height;public Rectangle() { origin = new Point(0,0); width = height = 0; }public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; }public void MoveTo (Point p) { origin = p; } }
Rectangle r = new Rectangle(new Point(10, 20), 5, 5);int area = r.width * r.height;r.MoveTo(new Point(3, 3));
Differences Between Classes and Structs Classes Reference Types (objects stored on the heap) support inheritance (all classes are derived from