This document provides an introduction to .NET and Core C# programming. It discusses the .NET Framework, Common Language Infrastructure (CLI), Common Type System (CTS), Common Language Specification (CLS), and Common Intermediate Language (CIL). It also covers Mono, building and running C# applications on different platforms, and using various IDEs like Visual Studio.
2. .NET
Framework
• So=ware
framework
developed
by
MS
– Runs
primarily
on
windows
• Benefits
– Support
for
numerous
programming
languages
– Language
integra2on
• You
can
mix
different
languages
– Comprehensive
base
class
library
– Apps
runs
on
top
of
virtual
machine
• Support
for
other
operaIng
systems
(in
theory)
– Great
tools:
Visual
Studio
5. CLI
• CLI
is
an
open
specificaIon
that
describes
executable
code
and
runIme
environment
• CLI
is
core
of
– Microso=
.NET
Framework
– Mono
(Open
Source)
– Portable.net
(Open
Source)
6. CTS,
CLS,
CIL
• Common
Type
System
(CTS)
– A
set
of
data
types
and
operaIons
that
are
share
by
all
CTS-‐compliant
programming
languages,
such
as
C#
and
VB
• Common
Language
SpecificaIon
(CLS)
– Set
of
base
rules
to
which
any
language
targeIng
the
CLI
should
conform.
• Common
Intermediate
Language
(CIL)
– Intermediate
language
that
is
abstracted
from
the
plaTorm
hardware
(In
Java:
class)
13. Compiling
Several
Files
in
C#
C:CodeSample> csc /main:A /out:example.exe A.cs B.cs
C:CodeSample> example.exe
Hello World from class A
C:CodeSample> csc /main:B /out:example.exe A.cs B.cs
C:CodeSample> example.exe
Hello World from class B
14. Open
Source
DistribuIons
• h_p://www.mono-‐project.com
• h_p://www.gnu.org/so=ware/dotgnu/
pnet.html
15. IDEs
• You
can
compile
and
run
apps
in
commandline
– Several
good
text
editors:
notepad++,
sublime
text,
emacs
• SharpDevelop
– Open
source
IDE
for
.NET
• MS
Visual
Studio
Express
– Object
browsing
tools,
GUI
ediIng,
Intellisense
17. C#
• C#
is
very
similar
to
Java
– C#,
Java,
ObjecIve-‐C
and
C++
are
a
member
of
C
family
of
programming
languages
• C#
provides
more
syntax
than
Java,
although
Java
7
(and
upcoming
8)
is
“catching
up”
• In
this
course,
we
focus
on
C#
(instead
of
other
CIL
complient
languages)
18. About
Namespaces
• Libraries
give
you
exisIng
code
to
use
• In
.NET:
namespaces
– Using
System.CollecIons;
– Using
System.IO;
– Using
System.XML;
• System
is
not
the
root,
you
may
find
namespaces
like
Microso=
that
provides
services
unique
to
Windows
(can’t
run
on
other
systems)
• Namespaces
are
linked
to
files,
assemblies.
One
assembly
(mscorlib.dll)
can
contain
number
of
namespaces.
20. Adding
a
Assembly
in
VS
• When
wriIng
– Using
System.Windows.Forms;
• This
uses
a
assembly
System.Windows.Forms.dll
• You
must
add
this
as
a
reference
in
your
project!
• How?
– In
VS:
SoluIon
Explorer
>
References
>
Add
Reference
21. C#
using System;
class MyApp
{
public static void Main()
{
Calculator c = new Calculator();
Console.Write(c.calculate(5,5));
}
}
class Calculator {
public int calculate(int a, int b)
{
return a + b;
}
}
23. About
Windows
8
• Visual
Studio
2012
provides
templates
for
Win
8
app
development
• EnIrely
new
runIme:
Windows
Run2me
(WinRT)
• New
namespaces,
starIng
with
Windows
• App
development
very
similar
to
building
a
.NET
app:
can
be
constructed
using
C#,
visual
basic,
JS
or
C++
• Heavy
usage
of
XML-‐based
grammar
XAML
24. .NET
Role
under
Win8
• In
addiIon
of
using
Windows.*
namespaces
you
can
use
large
subset
of
.NET
plaAorm
• We
are
not
focusing
on
this,
but
exclusively
on
.NET
26. Visual
Studio
Tips
• Refer
to
external
assemblies?
– VIEW
>
SoluIon
Explorer
• UIliIty
for
invesIgaIng
assemblies
– VIEW
>
Object
Browser
UIlity
• Project
properIes?
– VIEW
>
SoluIon
Explorer
>
ProperIes
• Class
Tree
View?
– VIEW
>
Class
View
27. Code
Refactoring
and
Snippets
• Right
click
on
code
and
choose
refactor
– Extract
method
– Encapsulate
Field
– Extract
Interface
– …
• When
wriIng
code,
VS
make
suggesIons.
When
finding
the
right
one,
press
tab
twice.
– Press
esc
to
exit
28. DocumentaIon
• .NET
Framework
4.5
documentaIon
available
online:
– h_p://msdn.microso=.com/library
– Use
the
tree
hierarchy
leE
to
navigate
30. Keywords
• Single
rooted
class
hierarchy:
all
objects
inheritate
System.Object
• Almost
every
keyword
in
Java
can
be
found
from
C#
– super -> base
– instanceof -> is
– import -> using
– extends / implements -> :
• Otherwise,
pre_y
much
the
same
31. Memory
Handling
and
RunIme
• Memory
Handling
– Most
objects
in
C#
to
heap
using
new
– CLR
handles
garbage
collecIons
• RunIme
– C#
is
compiled
to
intermediate
langage
(IL)
– IL
runs
on
top
of
CLR
– IL
code
is
always
naIvely
compiled
before
running
36. Formatng
Console
Output
• Supports
prinT()
statement
of
C.
– Console.WriteLine(“Hello
{0}!
You
are
{1}
years
old”,
userName,
userAge);
• Can
use
formatng
– //
000099999
– Console.WriteLine(“{0:d9}”,
99999);
– c,
format
currency
– d,
format
decimal
numbers
– f,
format
fixed
point
• Can
be
used
also
in
Strings:
– string
message
=
string.Format(“{0:d9}”,
99999);
37. Data
Types
• Datatypes
are
“snippets”
for
full
blown
types
in
namespaces!
• bool
=>
System.Boolean
• byte
=>
System.Byte
• short,
int,
long,
char,
float,
double,
decimal
(128
bit),
string,
Object
• So
this
works!
– Console.Write(12.toString());
38. Members
of
Datatypes
• int.MaxValue
• int.MinValue
• double.maxValue
• double.minValue
• char.isDigit(myChar)
• char.isLe_er(myChar)
• bool
b
=
bool.Parse(“true”);
• double
d
=
double.Parse(“99.9”);
40. VerbaIm
Strings
String longstring = @”this is
a very long string
as you can see”;
Console.WriteLine(longstring);
41. Comparing
• Usually
we
compare
memory
addresses
– object1
==
object2
• When
using
strings,
this
is
not
the
case.
You
can
compare
strings
like
this
(unlike
in
Java)
– string1
==
string2
42. Immutable
• Strings
are
immutable!
Once
assigned
it
cannot
be
changed..
– String
newString
=
original.toUpper();
• Even
in
here
– string
s1
=
“a”;
– s1
=
“b”;
• A
new
string
is
created!
• When
use
of
heavy
textual
data,
don’t
use
strings.
Use
StringBuilder.
43. Implicitly
typed
variables
• Usually
we
do
this
– int
a
=
5;
– bool
b
=
true;
• But
we
can
do
also
– var
a
=
5;
– var
b
=
true;
• Why?
Think
of
this:
– var
c
=
SomeMethod();
44. C#
For
each
string [] array = {“a”, “b”, “c”};
foreach(string c in array) {
Console.WriteLine(c);
}