c# for programmers. using system; class hello { static void main() { console.writeline("hello...
TRANSCRIPT
![Page 1: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/1.jpg)
C#for Programmers
![Page 2: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/2.jpg)
using System;
class Hello { static void Main() { Console.WriteLine("Hello world"); }}
Hello World
import System;
System.out.println
![Page 3: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/3.jpg)
public static void Main() { ...}
public static int Main() { ... return 0;}
public static int Main(string[] args) { ... return 0;}
More Choices
![Page 4: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/4.jpg)
Command Line Compiler
Create source file: Hello.cs. Invoke compiler: csc Hello.cs If OK, Hello.exe is created. Run executable: Hello
![Page 5: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/5.jpg)
C# Program Structure
Namespaces Types, namespaces
Type declarations Classes, interfaces, structs, enums, delegates
Members Fields, methods, constants, indexers,
properties events, operators, constructors, destructors
package
Java Beans Void Finalize ( )
inner classes
![Page 6: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/6.jpg)
#define Dutch
using System;
public class Preprocessor { public static void Main() { #if Dutch Console.WriteLine(“Hallo Wereld"); #else Console.WriteLine(“Hello World"); #endif }}
Preprocessor
![Page 7: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/7.jpg)
namespace N1 { // N1 class C1 { // N1.C1 class C2 {} // N1.C1.C2 } namespace N2 { // N1.N2 class C2 {} // N1.N2.C2 } }
Namespace = Packets Made Easy
![Page 8: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/8.jpg)
using System;
namespace MyMethods { public class AddClass { public static long Add(long i, long j) { return(i+j); } }}
using System;
namespace MyMethods { public class MultiplyClass { public static long Multiply(long i, long j) { return(i*j); } }}
Creating DLLs
csc/target:library/out:MyLibrary.DLL
Add.cs Mult.cs
![Page 9: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/9.jpg)
using System; using MyLibrary;class MyClient { public static void Main() { long sum = AddClass.Add(10, 10); Console.WriteLine(sum); long product = MultiplyClass.Multiply(10, 10); Console.WriteLine(product); }}
Using DLLs
csc /reference:MyLibrary.DLLMyClient.cs
![Page 10: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/10.jpg)
Value Types
primitives int i;
enums enum State { Off, On }
structs struct Point { int x, y; }
Stored "on the stack" or "in-line."
![Page 11: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/11.jpg)
Enums
enum Suit { Clubs = 0; Diamonds = 1; Hearts = 2; Spades = 3; }
…Suit s = Suit.Clubs;
Console.WriteLine (s);
…
![Page 12: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/12.jpg)
Typesafe Enums in Java
Define a class representing a single element of the enumerated type without public constructors; that is, provide public static final fields for each constant in the enumerated type.
Remember that most programmers are lazy …. (and it is inefficient as well).
![Page 13: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/13.jpg)
Typesafe Enums in Java
public class Suit { private final int s; private Suit(int s) { this.s = s;
}
public int toint() { return s;
} public static final Suit CLUBS = new Suit(0); public static final Suit DIAMONDS = new Suit(1); public static final Suit HEARTS = new Suit(2); public static final Suit SPADES = new Suit(3); }
![Page 14: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/14.jpg)
try { throw new Exception(“Oops!”);} catch (Exception e) { … Handle exception ……;} finally { … clean up, even if no exception occurred…;}
Exceptions
Does not show up in type ofMethods
(no “throws” declaration)
![Page 15: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/15.jpg)
interface IFigure { int Area ();}
class Square : IFigure { private int side;
public Square (int side) { this.side = side; }
public int Area () { return (side*side); }}
Interfaces and Classes
![Page 16: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/16.jpg)
class B : A { public B (int x) : base (…,…,…) { ………… }
public B (bool b) : this (…,…,…) { ………… }
public B (char c) { ………… } }
Constructors
The optional constructor-initializer Is invoked before
executing the constuctorBody (default is base ( ) ).
![Page 17: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/17.jpg)
class X : IB, IC {
void () { Console.WriteLine ("IA.g"); }
void () { Console.WriteLine ("IC.f"); }
void () { Console.WriteLine ("IB.f"); }
}
IA.g
IB.f
IC.f
interface IA { void g ();}
interface IB : IA { void f ();}
interface IC : IA { void f ();}
Interfaces
![Page 18: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/18.jpg)
class Test { public static void Main () { X x = new X (); ((IA)x).g(); ((IC)x).f(); ((IB)x).f(); }}
Querying Interfaces
![Page 19: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/19.jpg)
X x = new X ();
if (x is IB) { IB b = (IB)x; ……………} else {}
as/is
![Page 20: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/20.jpg)
X x = new X ();
IB b = x as IB;if (b != null) { …………} else {}
as/is
![Page 21: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/21.jpg)
X x = new X ();
try { IB b = (IB)x; ……………} catch (InvalidCastException e) {}
as/is
![Page 22: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/22.jpg)
interface IB extends IA { void f ();}
interface IA { void g ();}
interface IC extends IA { void f ();}
class X implements IB, IC {
void () { Console.WriteLine ("g"); }
void () { Console.WriteLine ("f"); }}
g
f
Interfaces in Java
![Page 23: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/23.jpg)
using System;
namespace VirtualDog { public class Dog { public virtual void RollOver () { Console.WriteLine("Scratch my tummy."); Bark(); }
public virtual void Bark () { Console.WriteLine("WOOF WOOF (Dog)"); } }}
Thanks Roger Sessions!
Breeding Dogs
![Page 24: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/24.jpg)
Imports SystemNamespace VirtualDog Public Class Mopje : Inherits Dog Public overrides Sub Bark () Console.WriteLine("WOEF WOEF (Mopje)") End Sub End ClassEnd Namespace
Breeding Dogs
![Page 25: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/25.jpg)
import VirtualDog;
var d = new Dog();var m = new Mopje();
d.RollOver();m.RollOver();
Breeding Dogs
![Page 26: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/26.jpg)
class Party { private int start; public int Start { get { return start; } set { start = value; } }}
Properties and Indexers
You can perform arbitrarycomputation in get and set blocks
![Page 27: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/27.jpg)
class Demo { public static Main () { Borrel b = new Party (); b.Start = 3; int x = b.Start; }}
Properties and Indexers
![Page 28: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/28.jpg)
class Party { private Dictionary participants; public Borrel() { participants = new Dictionary(); } public bool this[String name] { get { return (participants.Contains(name) && (bool)participants[name]); } set { participants.Add(name,value); } }}
Properties and Indexers
![Page 29: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/29.jpg)
class Demo { public static void Main () { Borrel b = new Party (); b[“Erik”] = true; Console.WriteLine(b[“Bill”]); }}
Properties and Indexers
![Page 30: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/30.jpg)
Events
Call in
Call out
![Page 31: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/31.jpg)
Declaration
Instantiation & Invocation
delegate void D();
class Delegates { static void F(){System.Console.WriteLine(“F");} void G(){System.console.WriteLine(“G”);}
static void Main() { D f = new D(F); D g = new D(G); f(); g(); } }
Delegates ↔ Inner Classes
![Page 32: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/32.jpg)
Declare an interface to represent the delegate type and an (anonymous) class that implements this interface to represent each concrete delegate value:
delegate void D();…static void F(){ …} …D f = new D(F);
interface D { void F();}…D f = new D () { void F () { …. }}
Delegates in Java
![Page 33: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/33.jpg)
public delegate void TroubleHandler(Dog sender);
class Dog {
public event TroubleHandler OnTrouble;
TriggerTrouble () { if (OnTrouble != null) { OnTrouble (this); } }}
Dog d = new Dog();d.OnTrouble += new TroubleHandler (…);
Events = Notifications for which Clients Can Attach Event Handlers
![Page 34: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/34.jpg)
public static result-type operator binary-operator ( op-type operand , op-type2 operand2 ) { … }
public static result-type operator unary-operator ( op-type operand) { …}
Operator Overloading
![Page 35: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/35.jpg)
public static implicit operator conv-type-out ( conv-type-in operand ) { …}
public static explicit operator conv-type-out ( conv-type-in operand ) { …}
Coercion Overloading
![Page 36: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/36.jpg)
namespace List { using System.Xml.Serialization;
[ XmlRoot ( "List“ , Namespace="http://www.meijcrosoft.com" , IsNullable=false ) ] public class List { [ XmlElement("head", IsNullable=false)] public string Head; [XmlElement("tail", IsNullable=false)] public List Tail; } }
Attributes
![Page 37: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/37.jpg)
csc /target:library List.cs
Xsd List.dll
AttributesAttributes
![Page 38: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/38.jpg)
<?xml version="1.0" encoding="utf-8"?><schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="www.meijcrosoft.com" xmlns="http://www.w3.org/2001/XMLSchema"> <element name="List" xmlns:q1="www.meijcrosoft.com" type="q1:List" /> <complexType name="List"> <sequence> <element minOccurs="1" maxOccurs="1" name="head" type="string" /> <element minOccurs="1" maxOccurs="1" name="tail" xmlns:q2="www.meijcrosoft.com" type="q2:List" /> </sequence> </complexType></schema>
Attributes
![Page 39: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/39.jpg)
[ AttributeUsage ( AttributeTargets.Class , AllowMultiple = false )]public class MyAttribute : Attribute { public string msg; public MyAttribute (string msg) { this.msg = msg; } }
Your Own
Where can it occur?
Base class for attributes
![Page 40: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/40.jpg)
[MyAttribute(“Hello World”)]class Example { public static void Main () { object[] Attrs = Attribute.GetCustomAttributes (typeOf (Example)); MyAttribute a = (MyAttribute)Attrs[0]; Console.WriteLine (a.Message); }}
Example
![Page 41: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/41.jpg)
using System;using System.Threading;class Test {
static void printA () { while (true) { Console.Write("A");} }
static void printB () { while (true) { Console.Write("B");} }
public static void Main () { Thread a = new Thread(new ThreadStart(printA)); Thread b = new Thread(new ThreadStart(printB)); a.Start(); b.Start(); }}
Create New Thread
AABBBBAAAABBBBBBAAAAAABBBABABBBAABABBBABBAAAABABABABABBBABBBABBBABBBBBBBABBABBBBBAAAAAAABBBABBABBBABBBBABABABBBABABABBABABBBAABAAABABBBABBBB
![Page 42: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/42.jpg)
lock(e) { ………….}
Locks and Critical Sections
Typically this to protectinstance variable, or
typeof (c) to protectstatic variable
Statements that youwant to run as acritical section
![Page 43: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/43.jpg)
tlbimp ComComponent.tlb NetComponent.dll
COM .NET
Type Library Importer (TlbImp.exe) Converts a COM type library into
equivalent .NET DLL
![Page 44: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/44.jpg)
Type Library Exporter (TlbExp.exe) Converts a .NET assembly to a COM type library
TlbExp NetComponent.dll ComComponent.tlb
.NET COM
![Page 45: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/45.jpg)
using System;using System.Window.Forms;using System.Drawing;
public class MyForm : Form { public MyForm() { this.Text = “Hello World”; }}
public class Demo { public static void Main() { Application.Run(new MyForm()); }
}
WinForms
![Page 46: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/46.jpg)
Button b = new Button ();b.Location = new Point (256,64);b.Text = “Click Me”;this.Controls.Add(b);
Controls
Control component on a form that displays information or accepts user input
No notion of layoutmanager
![Page 47: C# for Programmers. using System; class Hello { static void Main() { Console.WriteLine("Hello world"); } } Hello World import System; System.out.println](https://reader030.vdocuments.site/reader030/viewer/2022032414/56649eeb5503460f94bfcefa/html5/thumbnails/47.jpg)
public class HelloWorldForm : Form { private Button b = new Button() ; private void OnClick (object sender, EventArgs evArgs) { b.Text = ”Ouch!”; }
public HelloWorldForm() { b.Location = new Point(20, 10); b.Text = "Click Me!"; b.Click += new EventHandler(OnClick); this.Controls.Add(b); }}
Example