C# Basics 4. Code Elements 5. Organization 6. GUI 7. Demo 8. Conclusion
C# Basics 4. Code Elements 5. Organization 6. GUI 7. Demo 8. Conclusion
C# Basics 4. Code Elements 5. Organization 6. GUI 7. Demo 8. Conclusion
1. Introduction
2. .NET Basics
3. C# Basics
4. Code Elements
5. Organization
6. GUI
7. Demo
8. Conclusion
1/8:Introduction
Introduction
Introduction
.NET Basics
.NET Basics
.NET Basics
.NET Basics
What is C#
What is C#
Types of
Application
The product of the C# compiler is called the
“Assembly”. It’s either a “.dll” or a “.exe”
file. Both run on the Common Language
Runtime and are different from native code
that may also end with a “.exe” extension.
using
System;
namespace
typical_trivial{
class
House{
private
int
location;
protected
string
name;
public
House(){
name
=
"No
Name
Yet!";
}
//
every
class
inherits
‘object’
that
has
ToString()
public
override
string
ToString(){
string
disp
=
"Name
is
"
+
name
+
",
location=
"
+
location.ToString();
return
disp;
}
}
Continues to the next slide …
3/8:C# Basics
3/3:Typical and Trivial
A Typical and Trivial
Program in C#
… continuing from the previous slide
class
Program{
static
void
Main(string[]
args){
House
h
=
new
House();
for
(int
i
=
0;
i
<
4;
i++){
System.Console.WriteLine("i={0},
house
says:
{1}",
i,
h.ToString());
}
System.Console.Read();
}
}
}
4/8:Code Elements
1/12:Types
Types
Types
Types
Nullable
The value types can’t be assigned a null. To
enable regular primitive value types to take a
null value, C# uses nullable types using ‘?’
with type name. Following example shows
how.
int?
a;
a
=
null;
int
b;
b
=
a
??
-‐99;
//
the
??
operator
picks
-‐99
if
null
System.Console.WriteLine("this
is
null.
{0}",b);
a
=
23;
//
not
null
System.Console.WriteLine("this
is
not
null.
{0}",
a
??
-‐99);
4/8:Code Elements
1/12:Types
Types
Anonymous
Variables can be defined without an explicit name
and to encapsulate a set of values. This is
useful for C#’s Language Integrated Query
(which will not be discussed in this
presentation)
var
a
=
3;
//
the
type
is
automatically
inferred
by
compiler
var
b
=
new
{
id
=
21,
name
=
"Tito"
};
System.Console.WriteLine("a={0},
b.id={1},
b.name={2}",
a,
b.id,
b.name);
4/8:Code Elements
2/12:Array
Arrays
Array
Properties
Properties
class
Client{
private
string
name
;
public
string
Name{
get{
return
name;
}
set{
name=value;
}
}
static
void
Main(string[]
args)
{
Client
c
=
new
Client();
c.Name
=
"Celia";
System.Console.WriteLine(c.Name);
System.Console.ReadLine();
}
}
4/8:Code Elements
3/12:Property
Properties
Automatically Implemented
C# also has a feature to automatically implement the getters
and setter for you.
Users have direct access to the data members of the class.
Following example does the same thing as the previous
example, but using automatically implemented properties
class
Client2{
public
string
Name
{
get;
set;
}
static
void
Main(string[]
args){
Client2
c
=
new
Client2();
c.Name
=
"Cruz";
System.Console.WriteLine(c.Name);
System.Console.ReadLine();
}
}
4/8:Code Elements
4/12:Indexer
Indexers
Nested Classes
Inheritance and
Interface
A class can directly inherit from only one base class and
can implement multiple interfaces.
To override a method defined in the base class, the
keyword ‘override’ is used.
An abstract class can be declared with the keyword
‘abstract’.
A static class is a class that is declared with the ‘static’
keyword. It can not be instantiated and all
members must be static.
4/8:Code Elements
6/12:Inheritance and Interface
Inheritance and
Interface
class
BaseClass{
public
virtual
void
show(){
System.Console.WriteLine("base
class");}
}
interface
Interface1{void
showMe();}
interface
Interface2{void
showYou();}
class
DerivedAndImplemented:
BaseClass,Interface1,Interface2{
public
void
showMe()
{
System.Console.WriteLine("Me!");
}
public
void
showYou()
{
System.Console.WriteLine("You!");
}
public
override
void
show(){
System.Console.WriteLine("I'm
in
derived
Class");}
static
void
Main(string[]
args){
DerivedAndImplemented
de
=
new
DerivedAndImplemented();
de.show();
System.Console.Read();}
}
4/8:Code Elements
7/12:Class Access & Partial
Delegates
Delegates
class
Program
{
delegate
int
mydel(int
aa);
int
myfunc(int
a){
return
a*a;
}
int
myfunc2(int
a)
{
return
a
+
a;
}
static
void
Main(string[]
args)
{
Program
p=new
Program();
mydel
d=p.myfunc;
System.Console.WriteLine(d(5));
d
=
p.myfunc2;
System.Console.WriteLine(d(5));
System.Console.Read();
}
}
4/8:Code Elements
8/12:Delegate
Delegates
Lambda Expression
In C#, implementors (functions) that are targeted by a
delegate, can be created anonymously, inline and on
the fly by using the lambda operator “=>”.
class
Program
{
delegate
int
mydel(int
aa,
int
bb);
static
void
Main(string[]
args)
{
mydel
d
=
(a,
b)
=>
a
+
2
*
b;
//
in
above
line,
read
a,b
go
to
a+b*2
to
evaluate
System.Console.WriteLine(d(2,3));
System.Console.Read();
}
}
4/8:Code Elements
9/12:Generic
Generics
Generics
class
Genclass<T>{
public
void
genfunc(int
a,
T
b){
for
(int
i
=
0;
i
<
a;
i++){
System.Console.WriteLine(b);
}
}
}
class
Program{
static
void
Main(string[]
args){
Genclass<float>
p
=
new
Genclass<float>();
p.genfunc(3,(float)5.7);
System.Console.Read();
}
}
4/8:Code Elements
10/12:Object Initializer
Object Initializer
Object Initializer
class
Client2
{
public
string
Name
{
get;
set;
}
static
void
Main(string[]
args)
{
Client2
c
=
new
Client2
{Name="Adalbarto"};
//
above
is
the
object
initializer
System.Console.WriteLine(c.Name);
System.Console.ReadLine();
}
}
4/8:Code Elements
11/12:Iterator
Iterator
Iterator
public
class
MyBooks
:
System.Collections.IEnumerable
{
string[]
books
=
{
"Linear
Systems",
"Design
Patterns
Explained",
"The
Now
Habbit",
"The
DeVinci
Code"
};
public
System.Collections.IEnumerator
GetEnumerator()
{
for
(int
i
=
0;
i
<
books.Length;
i++)
{
yield
return
books[i];
}
}
}
class
Program
{
static
void
Main(string[]
args)
{
MyBooks
b
=
new
MyBooks();
foreach
(string
s
in
b)
{
System.Console.Write(s
+
"
");
}
System.Console.Read();
}
}
4/8:Code Elements
12/12:Sturcture
Structure
Structure
struct
Rectangle{
public
int
length;
public
int
width;
public
Rectangle(int
length,int
width){
this.length=length;
this.width=width;
}
public
int
getArea(){
return
length*width;
}
}
class
Program{
static
void
Main(string[]
args){
Rectangle
r=new
Rectangle(2,5);
System.Console.WriteLine("The
area
is:
{0}",r.getArea());
System.Console.Read();
}
}
5/8:Organization
1/4:Namespaces
Namespace
Namespace
using
System;
namespace
space1{
class
MyClass1{
public
void
show(){
System.Console.WriteLine("MyClass1");
}
}
}
namespace
space2{
class
Program{
static
void
Main(string[]
args){
space1.MyClass1
c=new
space1.MyClass1();
c.show();
Console.Read();
}
}
}
5/8:Organization
2/4:Attribute
Attribute
Attribute
The IDE
Other Miscellaneous
Items
• Use of pointers: C# can be configured to allow pointer
declaration and arithmetic.
• XML comments: It’s possible to follow the XML
comments syntax in code and the compiler will
generate a documentation for you based of those
comments and their location.
• Threading: It’s possible to write multi-threaded
programs using the System.Threading class library.
• C# allows easy integration to unmanaged code (outside
of .NET) such as Win32Api, COM,C++ programs etc to
it’s own.
5/8:Organization
4/4:Other Misc
Other Miscellaneous
Items
• C# allows editing the file system and the Windows
System Registry.
• Exception: Exceptions can be handled using C#’s try,
throw, catch.
• Collection Classes: These provide support for various
data structures such as list, queue, hash table etc.
• Application Domain: The context in which the assembly
is run is known as the application domain and is usually
determined by the Common Language Runtime (it is
also possible to handle this in code). This isolates
individual programs and provides security.
6/8:GUI
1/3:Introduction
GUI:
Introduction
The .NET framework provides a class library of
various graphical user interface tools such as
frame, text box, buttons etc. that C# can use
to implement a GUI very easily and fast.
Visual Items
Visual Items
The containing
window is
called the
“Frame”.
Inside are
some other
GUI
elements,
such as
Button,
TextBox etc
6/8:GUI
3/3:Events
1/4:Intro
Events
When anything of interest occurs, it’s called an
event such as a button click, mouse pointer
movement, edit in a textbox etc.
An event is raised by a class. This is called
publishing an event.
It can be arranged that when an event is raised,
a class will be notified to handle this event,
i.e. perform required tasks. This is called
subscribing to the event. This is done via:
• Delegates or
• Anonymous function or
• Lambda expression.
These will be discussed shortly
6/8:GUI
3/3:Events
1/4:Intro
Events
The .NET Framework has many built-in events
and delegates for easily subscribing to these
events by various classes.
For example, for Button class, the click event is
called “Click” and the delegate for handler is
called “System.EventHandler”. These are
already defined in the .NET class library.
To tie the event with the handler, the operator
“+=” is used. (Will be discussed shortly)
Then, when the Button object is clicked, that
function will execute.
6/8:GUI
3/3:Events
2/4:With Delegates
With Delegates
Class
Form1:Form{
private
System.Windows.Forms.Button
button1;
///...
Void
init(){
this.button1.Click
+=
new
System.EventHandler(this.button1_Click);
}
private
void
button1_Click(object
sender,
EventArgs
e){
button1.Text
=
"clicked1";
}
}
6/8:GUI
3/3:Events
3/4:With Lambda
With Lambda
Expression
The following code segment shows the
subscription of the event Button.Click by
inline code which is defined using the lambda
operator.
This program does the same thing as the last.
Class
Form1:Form{
private
System.Windows.Forms.Button
button1;
///...
Void
init(){
//
the
arguments
a,b
are
just
to
satisfy
the
delegate
signature.
//
they
do
nothing
useful
in
this
simple
example.
this.button1.Click
+=
(a,b)
=>
{
this.button1.Text
=
"clicked1";
};
}
}
6/8:GUI
3/3:Events
4/4:With Anonymous Method
With Anonymous
Methods
An anonymous method is declared with the
keyword “delegate”. It has no name in
source code level.
After the delegate keyword, the arguments need
to be put in parenthesis and the function
body needs to be put in braces.
It is defined in-line exactly where it’s instance is
needed.
Anonymous methods are very useful in event
programming in C# with .NET Framework.
6/8:GUI
3/3:Events
4/4:With Anonymous Method
With Anonymous
Methods
The following example does the same thing as
the previous two examples but uses
anonymous methods to handle that event.
Class
Form1:Form{
private
System.Windows.Forms.Button
button1;
///...
Void
init(){
this.button1.Click
+=
delegate(object
oo,
System.EventArgs
ee)
{
this.button1.Text
=
"clicked1";
};
}
}
7/8:Demo
Demo
Conclusion
Conclusion
Conclusion