Escolar Documentos
Profissional Documentos
Cultura Documentos
Development
Structure of a C# Program
// Specify namespaces we use classes from here
using System;
using System.Threading; // Specify more specific namespaces
namespace AppNamespace
{
// Comments that start with /// used for
// creating online documentation, like javadoc
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
// .. Code for class goes here
}
}
Defining a Class
class Class1
{
static void Main(string[] args)
{
// Your code would go here, e.g.
Console.WriteLine("hi");
}
/* We can define other methods and vars for the class */
// Constructor
Class1()
{
// Code
}
// Some method, use public, private, protected
// Use static as well just like Java
public void foo()
{
// Code
}
// Instance, Static Variables
private int m_number;
public static double m_stuff;
}
C# Basics
Getting Help
If MSDN is installed
http://msdn.microsoft.com/library/default.asp
You will have to drill down to VS.NET, Documentation, VB
and C#, and then to the C# reference.
will output:
hello world 5
We can also use {0}, {1}, {2}, etc. to indicate
arguments in the WriteLine statement to print. For
example:
Console.WriteLine(hi {0} you are {0} and your age is {1},
Kenrick, 23);
will output:
hi Kenrick you are Kenrick and your age is 23
WriteLine Options
outputs as:
you have $1.30 dollars.
Data Types
C# supports value
types and
reference types.
Built-In Types
C
#
T
y
p
e
b
o
o
l
b
y
te
s
b
y
te
c
h
a
r
d
e
c
im
a
l
d
o
u
b
le
flo
a
t
in
t
u
in
t
lo
n
g
u
lo
n
g
o
b
je
c
t
s
h
o
r
t
u
s
h
o
r
t
s
tr
in
g
.N
E
T
F
r
a
m
e
w
o
r
k
t
y
p
e
S
y
s
t
e
m
.B
o
o
le
a
n
S
y
s
t
e
m
.B
y
t
e
S
y
s
t
e
m
.S
B
y
t
e
S
y
s
t
e
m
.C
h
a
r
S
y
s
t
e
m
.D
e
c
im
a
l
S
y
s
t
e
m
.D
o
u
b
le
S
y
s
t
e
m
.S
in
g
le
S
y
s
t
e
m
.I
n
t
3
2
S
y
s
t
e
m
.U
I
n
t
3
2
S
y
s
t
e
m
.I
n
t
6
4
S
y
s
t
e
m
.U
I
n
t
6
4
S
y
s
t
e
m
.O
b
j
e
c
t
S
y
s
t
e
m
.I
n
t
1
6
S
y
s
t
e
m
.U
I
n
t
1
6
S
y
s
t
e
m
.S
t
r
in
g
Automatic Boxing/Unboxing
G
e
t
T
y
p
e
T
o
S
t
r
i
n
g
O
v
e
r
l
o
a
d
e
d
.D
e
t
e
r
m
i
n
e
sw
h
e
t
h
e
rt
w
o
O
b
j
e
c
ti
n
s
t
a
n
c
e
sa
r
e
e
q
u
a
l
.
S
e
r
v
e
sa
sa
h
a
s
h
f
u
n
c
t
i
o
n
f
o
ra
p
a
r
t
i
c
u
l
a
r
t
y
p
e
,s
u
i
t
a
b
l
e
f
o
ru
s
e
i
n
h
a
s
h
i
n
g
a
l
g
o
r
i
t
h
m
s
a
n
d
d
a
t
a
s
t
r
u
c
t
u
r
e
sl
i
k
e
a
h
a
s
h
t
a
b
l
e
.
G
e
t
st
h
e
T
y
p
e
o
ft
h
e
c
u
r
r
e
n
ti
n
s
t
a
n
c
e
.
R
e
t
u
r
n
sa
S
t
r
i
n
g
t
h
a
tr
e
p
r
e
s
e
n
t
st
h
e
c
u
r
r
e
n
t
O
b
j
e
c
t
.
Structures
x = p1;
y = p2;
Enumeration Type
Example:
// Enum goes outside in the class definition
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
// Inside some method
Days day1, day2;
int day3;
day1 = Days.Sat;
day2 = Days.Tue;
day3 = (int) Days.Fri;
Console.WriteLine(day1);
Console.WriteLine(day2);
Enumeration
Console.WriteLine(day3);
Output:
Tue
6
Sat
really
maps to Int as the
underlying data type
Strings
Classes
Class
Example
Class Notes
Interfaces
Sample Interface
public interface IDrivable {
void Start();
void Stop();
void Turn();
}
public class SportsCar : IDriveable {
void Start() {
// Code here to implement start
}
void Stop() {
// Code here to implement stop
}
void Turn() {
// Code here to implement turn
}
}
Reading Input
Procedural Stuff
Random class:
Random r = new Random();
r.NextDouble();
// Returns random double between 01
r.Next(10,20);
// Random int, 10 int < 20
Passing Parameters
Passing by Reference
Arrays
For example:
int arr = new int[100];
More on Arrays
This would assign the reference, not make a copy of the array.
To copy the array we must copy each element one at a time, or use
the Clone() method to make a copy of the data and set a new
reference to it (and garbage collect the old array values).
Multidimensional Arrays
Related to Arrays
Delegates
Delegates Example
Compare1 uses alphabetic comparison, Compare2 uses length
/
/
T
w
o
d
i
f
f
e
r
e
n
t
m
e
t
h
o
d
s
f
o
r
c
o
m
p
a
r
i
s
o
n
p
u
b
l
i
c
s
t
a
t
i
c
i
n
t
c
o
m
p
a
r
e
1
(
s
t
r
i
n
g
s
1
,
s
t
r
i
n
g
s
2
)
{
r
e
t
u
r
n
(
s
1
.
C
o
m
p
a
r
e
T
o
(
s
2
)
)
;
}
p
u
b
l
i
c
s
t
a
t
i
c
i
n
t
c
o
m
p
a
r
e
2
(
s
t
r
i
n
g
s
1
,
s
t
r
i
n
g
s
2
)
{
i
f
(
s
1
.
L
e
n
g
t
h
<
=
s
2
.
L
e
n
g
t
h
)
r
e
t
u
r
n
1
;
e
l
s
e
r
e
t
u
r
n
1
;
}
Delegates Example
p
u
b
l
i
c
d
e
l
e
g
a
t
e
i
n
t
C
o
m
p
a
r
e
D
e
l
e
g
a
t
e
(
s
t
r
i
n
g
s
1
,
s
t
r
i
n
g
s
2
)
;
/
/
A
m
e
t
h
o
d
t
h
a
t
u
s
e
s
t
h
e
d
e
l
e
g
a
t
e
t
o
f
i
n
d
t
h
e
m
i
n
i
m
u
m
p
u
b
l
i
c
s
t
a
t
i
c
s
t
r
i
n
g
F
i
n
d
M
i
n
(
s
t
r
i
n
g
[
]
a
r
r
,
C
o
m
p
a
r
e
D
e
l
e
g
a
t
e
c
o
m
p
a
r
e
)
{
i
n
t
i
,
m
i
n
I
n
d
e
x
=
0
;
f
o
r
(
i
=
1
;
i
<
a
r
r
.
L
e
n
g
t
h
;
i
+
+
)
{
i
f
(
c
o
m
p
a
r
e
(
a
r
r
[
m
i
n
I
n
d
e
x
]
,
a
r
r
[
i
]
)
>
0
)
m
i
n
I
n
d
e
x
=
i
;
}
r
e
t
u
r
n
a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