Csharp Dotnet Adnanreza
Csharp Dotnet Adnanreza
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 t y p i c a l _ t r i v i a l {
class House{
private i n t location;
protected s t r i n g name;
p u b l i c House(){
name = "No Name Ye t ! " ;
}
/ / every class i n h e r i t s ‘object’ t h a t has ToSt r ing()
p u b l i c override s t r i n g To S t r i n g ( ) {
s t r i n g disp = "Name i s " + name + " , location= " +
l o c a t i o n . To S t r i n g ( ) ;
return disp;
}
}
Continues to the next slide …
3/8:C# Basics
3/3:Typical and Trivial
A Typical and
TrivialProgram in C#
… continuing from the previous slide
class Program{
s t a t i c void M a i n ( s t r i n g [ ] a r g s ) {
House h = new House();
for ( i n t i = 0; i < 4; i++){
System.Console.WriteLine( "i
={0}, house says:
{ 1 } " , i , h . To S t r i n g ( ) ) ;
}
System.Console.Read();
}
}
}
4/8:Code Elements
1/12:Types
Types
1. Value type
1. Variable name contains the actual value
2. int, double and other primitive types
3. Structure, Enumeration, etc.
2. Reference Type
1. Variable name contains the reference or
pointer to the actual value in memory
2. Array, derived from class Array
3. Class, Interface, Delegate, String, etc.
4/8:Code Elements
1/12:Types
Type
s
• The value types are derived from
System.ValueType
• All types in C# are derived from
System.Object which is also accessed
by the alias keyword ‘object’
• This type hierarchy is called Common
Type System (CTS)
4/8:Code Elements
1/12:Types
Type
Nullable
s
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.
i n t ? a ; a = n u l l ; i n t b ; b = a ?? -99;‐
/ / the ?? operator picks -9 ‐9 i f n u l l
System.Console.WriteLine( "this i s n u l l . { 0 } " , b ) ;
a = 23; / / not n u l l
System.Console.WriteLine( "this i s not n u l l . { 0 } " ,
a ?? -99);
‐
4/8:Code Elements
1/12:Types
Type
Anonymous
s
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)
Array
s
Following is an example of declaring and
using a simple array.
i n t [ ] items = new i n t [ ] { 5 , 1 9 , 4 1 , 1 , 9 } ;
foreach ( i n t i i n items)
{
System.Console.WriteLine( "{0}\n",
i-1);
‐
}
4/8:Code Elements
2/12:Array
Arra
y
The ‘foreach’ ,’in’ keywords are used
to provide read only (recommended)
access to members of an array or any
object implementing the IEnumerable
interface (more about this is discussed
in the section ‘Iterator’).
4/8:Code Elements
3/12:Property
Properties
Properties
class C l i e n t {
p r i v a t e s t r i n g name ;
p u b l i c s t r i n g Name{
get{
r e t u r n name;
}
set{
name=value;
}
}
s t a t i c void
M a i n ( s t r i n g [ ] args)
{
C l i e n t c = new C l i e n t ( ) ;
c.Name = " C e l i a " ;
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 C l i e n t 2 {
p u b l i c s t r i n g Name { g e t ; s e t ; }
s t a t i c void M a i n ( s t r i n g [ ] a r g s ) {
Cli ent 2 c = new C l i e n t 2 ( ) ;
c.Name = " Cr u z " ;
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{
p u b l i c v i r t u a l void show()
{ System.Console.WriteLine("base
class");}
}
i n t e r f a c e I n t e r f a c e 1 { v o i d showMe();}
i n t e r f a c e I n t e r f a c e 2 { v o i d showYou();}
class DerivedAndImplemented:
BaseClass,Interface1,Interface2{ p u b l i c void showMe()
{ System.Console.WriteLine("Me!"); } p u b l i c void showYou() {
System.Console.WriteLine( "You!"); } p u b l i c overri de void
show(){
System.Console.WriteLine("I'm i n derived C l a s s " ) ; }
s t a t i c void M a i n ( s t r i n g [ ] a r g s ) {
DerivedAndImplemented de = new DerivedAndImplemented();
de.show();
System.Console.Read();}
}
4/8:Code Elements
7/12:Class Access & Partial
Delegates
Delegates
class Program
{
delegate i n t mydel (i nt a a ) ;
i n t myfunc(int a ) { r e t u r n a* a; }
i n t myfunc2(int a ) { r e t u r n a + a ; }
s t a t i c void M a i n ( s t r i n g [ ] 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 i n t mydel ( int aa, i n t b b ) ;
s t a t i c void M a i n ( s t r i n g [ ] args) {
mydel d = ( a , b) => a + 2 * b ;
/ / i n above l i n e , read a,b go t o a+b*2
t o evaluate
System.Console.WriteLine(d(2,3));
System.Console.Read();
}
}
4/8:Code Elements
9/12:Generic
Generics
Generics
class Genclass<T>{
p u b l i c void genfunc(i nt a , T b )
{ for ( i n t i = 0; i < a; i++)
{
System.Console.WriteLine(b);
}
}
}
class Program{
s t a t i c void M a i n ( s t r i n g [ ] a r g s )
{ Genclass<float> p = new
Genclass< float>(); p . g e n f u n c ( 3 ,
(float)5.7);
System.Console.Read();
}
}
4/8:Code Elements
10/12:Object Initializer
Object Initializer
Object Initializer
class C l i ent 2
{
p u b l i c s t r i n g Name { g e t ; s e t ; }
s t a t i c void M a i n ( s t r i n g [ ] args)
{
C l i ent 2 c = new Cl i e nt 2
{Name="Adalbarto"};
/ / above i s the obj ect i n i t i a l i z e r
System.Console.WriteLine(c.Name);
System.Console.ReadLine();
}
}
4/8:Code Elements
11/12:Iterator
Iterator
Iterator
p u b l i c class MyBooks : System.Collections.IEnumerable
{ s t r i n g [ ] books = { "Li near Systems", "Design
Patterns
Explained", "The Now H a b b i t " , "The DeVinci Code" } ;
p u b l i c System.Collections.IEnumerator GetEnumerator()
{ f o r ( i n t i = 0 ; i < books.Length; i + + ) {
y i el d return books[ i];
}
}
}
class Program {
s t a t i c void M a i n ( s t r i n g [ ] args) {
MyBooks b = new MyBooks();
foreach ( s t r i n g s i n b) {
System.Console.Write(s + "
");
}
System.Console.Read();
}
}
4/8:Code Elements
12/12:Sturcture
Structure
Structure
s t r u c t Rectangle{
public i n t length; public i n t width;
p u b l i c Rectangle(int l e n g t h , i n t w i d t h ) {
this.length=length;
this.width=width;
}
p u b l i c i n t getA rea()
{ return
l e n g t h * w i d t h;
}
}
class Program{
s t a t i c void M a i n ( s t r i n g [ ]
args){
Rectangle r=new Rectangle(2,5); System.Console.WriteLine("The
area i s : { 0 } " , r. g e t A r e a ( ) ) ; System.Console.Read();
}
}
5/8:Organization
1/4:Namespaces
Namespace
Namespace
using System;
namespace space1{
class
MyClass1{
p u b l i c void show()
{ System.Console.WriteLine("MyClass1
");
}
}
}
namespace
space2{ class
Program{
s t a t i c void
Main(stri
ng[] args)
{
space1.MyClass1 c=new space1.MyClass1();
c.show(); Console.Read();
}
5/8:Organization
2/4:Attribute
Attribute
Attributes add metadata to the code entities such
as assembly, class, method, return value etc
about the type.
This metadata describes the type and
it’s members
This metadata is used by the Common Runtime
Environment or can also be used by client
code.
Attributes are declared in square brackets above
the class name, method name etc.
5/8:Organization
2/4:Attribute
Attribute
The IDE
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{
p r i v a t e System.Windows.Forms.Button button1;
///...
Void i n i t ( ) {
t h i s . b u t t o n 1 . C l i c k += new System.EventHandler(this.button1_Click);
}
p r i v a t e void button1_Click(object sender, EventArgs e )
{ button1.Text = " c l i c k e d 1 " ;
}
}
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{
p r i v a t e System.Windows.Forms.Button button1;
///...
Void i n i t ( ) {
/ / the arguments a,b are j u s t t o s a t i s f y the delegate s i g n a t u r e .
/ / they do nothing us ef ul i n t h i s simple example. t h i s . b u t t o n 1 . C l i c k
+= ( a , b ) => { t h i s . b u t t o n 1 . Te x t = " c l i c k e d 1 " ; } ;
}
}
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{
p r i v a t e System.Windows.Forms.Button button1;
///...
Void i n i t ( ) {
t h i s . b u t t o n 1 . C l i c k += delegate(object oo, System.EventArgs ee) {
t h i s . b u t t o n 1 . Te x t = " c l i c k e d 1 " ;
};
}
}
7/8:Demo
Demo
Conclusion
Conclusion
Conclusion