Академический Документы
Профессиональный Документы
Культура Документы
// Demonstrating C# properties.
class SimpProp
{
int prop;
public SimpProp()
{
prop = 0;
}
public int MyProp
{
get
{
return prop;
}
set
{
if (value >= 0)
prop = value;
else
Console.WriteLine("U r trying to assign a -ve value...!");
}
}
}
class PropertyDemo
{
public static void Main()
{
SimpProp ob = new SimpProp();
Console.WriteLine("Original value of ob.MyProp: " + ob.MyProp);
ob.MyProp = 100; // assign value
Console.WriteLine("Value of ob.MyProp: " + ob.MyProp);
Console.WriteLine("Attempting to assign -10 to ob.MyProp");
ob.MyProp = -10;
Console.WriteLine("Value of ob.MyProp: " + ob.MyProp);
}
}
The sender of the events (the Car type in this case) will make calls on
the sink under the appropriate circumstances.
// Car event sink.
public class CarEventSink : IEngineEvents
{
private string name;
public CarEventSink() { }
public CarEventSink(string sinkName)
{
name = sinkName;
}
public void AboutToBlow(string msg)
{
Console.WriteLine("{0} reporting: {1}", name, msg);
}
public void Exploded(string msg)
{
Console.WriteLine("{0} reporting: {1}", name, msg);
}
GRNICA
}
Now we have a sink object that implements the event interface, our
next task is to pass a reference to this sink into the Car type.
The Car holds onto the reference and makes calls back on the sink.
In order to allow the Car to obtain a reference to the sink, we will
need to add a public helper member to the Car type that we will call
Advise().
Likewise, if the caller wishes to detach from the event source, it may
call another helper method on the Car type named Unadvise().
In order to allow the caller to register multiple event sinks, the Car
now maintains an ArrayList to represent each outstanding
connection:
GRNICA
class Car
{
int currSpeed, maxSpeed;
String name;
// The set of connected sinks.
ArrayList clientSinks = new ArrayList();
// Attach or disconnect from the source of events.
public void Advise(IEngineEvents sink)
{
clientSinks.Add(sink);
}
public void Unadvise(IEngineEvents sink)
{
clientSinks.Remove(sink);
}
public Car(String petName, int curSpeed, int maxspeed)
{
name = petName;
currSpeed = curSpeed;
maxSpeed = maxspeed;
}
GRNICA
GRNICA
10
GRNICA
12
Delegate Declaration
Delegate methods definition
Delegate Instantiation
Delegate Invocation
The delegate declaration defines a class using the class
System.Delegate as a base class.
GRNICA
13
Delegate Declaration
Modifier delegate return-type delegate-name (parameters);
Delegate is a keyword that signifies that the declaration represents a
class type derived from System.Delegate.
14
The use of the keyword delegate tells the compiler that it is the
definition of a new class using the System.Delegate as the base
class.
GRNICA
15
Delegate Methods
The methods whose references are encapsulated into a delegate
instance are known as delegate methods or callable entities.
The signature and return type of the methods must exactly match the
signature and return type of the delegate.
For instance, the delegate
delegate string GetString()
can be made to refere to the method ToString() using an int as follows
int N = 100;
GetAString s1 = new GetAString(N.ToString);
GRNICA
16
The delegate
delegate void Delegate1();
can encapsulate references to the following methods :
public void F1()
{
Console.WriteLine("F1"); }
static public void F2()
{
Console.WriteLine("F2"); }
The delegate
delegate double MathOp(double x, double y);
can refere to any of the following methods:
public static double Multiply(double a, double b)
{
return (a*b);
}
public double Divide(double a, double b)
{
return (a/b);
}
GRNICA
17
GRNICA
18
Multicast Delegates
So far the delegate has invoked only one method.
It is possible for certain delegates to hold and invoke multiple
methods. Such delegates are called as multiple delegaets
(combinable delegates).
But it must satisfy following conditions :
the return type of the delegate must be void.
19
GRNICA
20
GRNICA
21
22
EventHandler and RateChange are delegates and Click & Rate are
events.
GRNICA
23
When you wish to detach from a source of events, use the -= operator:
ObjectVariable.EventName -= delegateObject;
GRNICA
24
25
26
GRNICA
27
28
29
// overloading + operator
class Complex
{
double x, y;
public Complex()
{
}
public Complex(double real, double imag)
{
x = real;
y = imag;
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex c3 = new Complex();
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return (c3);
}
public void display()
{
Console.Write(x);
Console.WriteLine("+j" + y);
Console.WriteLine();
}
GRNICA
}
30
class ComplexTest
{
static void Main(string[] args)
{
Complex a= new Complex(2.5,3.5);
Complex b= new Complex(1.6,2.7);
Complex c = a + b;
Console.Write(" a = ");
a.display();
Console.Write(" b = ");
b.display();
Console.Write(" c = ");
c.display();
}
}
GRNICA
31
General syntax :
element-type this[int index]
{
// The get accessor.
get {
// return the value specified by index
}
// The set accessor.
set {
// set the value specified by index
}