// とても簡単なイベントの例
using System;
// イベント用のデリゲートの宣言
delegate void MyEventHandler(int x); //←イベントで使うデリゲートを宣言する
// イベントの送信側のクラス
class MyEvent
{
public event MyEventHandler SomeEvent; //←イベントを宣言する
// イベント送信を実装したメソッド
public void Fire(int x)
{
if (SomeEvent != null)
SomeEvent(x); //←イベントを送信する
}
}
class H1
{
public void Handler(int x)
{
Console.WriteLine("1から" + x + "迄の和");
int wa = 0;
for (int i = 1; i <= x; i++)
wa += i;
Console.WriteLine(wa);
}
}
class H2
{
public void Handler2(int x)
{
Console.WriteLine("1から" + x + "迄の2乗の和");
int wa = 0;
for (int i = 1; i <= x; i++)
wa += i * i;
Console.WriteLine(wa);
}
}
class EventDemo
{
static void Main()
{
H1 objH1 = new H1();
H2 objH2 = new H2();
MyEvent evt = new MyEvent(); //←イベントを送信する側のインスタンスを作成する
// ハンドラをイベントリストに追加する
evt.SomeEvent += objH1.Handler; //←イベントのチェーンにハンドラを追加する
evt.SomeEvent += objH2.Handler2;
// イベントを人為的に発生させる
evt.Fire(20); //←イベントを発生させる
}
}
using System;
namespace Chapter01All
{
class Ano
{
public int Syukei(int l)
{
int w = 0;
for (int i = 1; i <= l; i++)
{
w += i;
}
return w;
}
}
class Demo
{
static void Main()
{
Ano obj = new Ano();
Console.WriteLine(obj.Syukei(10));
}
}
}
class AnoのインスタンスからSyukeiを呼ぶ場合。
using System;
namespace Chapter01All
{
class Ano
{
public static int Syukei(int l)
{
int w = 0;
for (int i = 1; i <= l; i++)
{
w += i;
}
return w;
}
}
class Demo
{
static void Main()
{
//Ano obj = new Ano();
Console.WriteLine(Ano.Syukei(10));
}
}
}
using System;
namespace Chapter01All
{
class MyClass
{
public MyClass()
{
Console.WriteLine("Creaing a MyClass instance.");
}
public void SayHello()
{
Console.WriteLine("今日は");
}
}
class Test : MyClass
{
public Test()
{
Console.WriteLine("Creating a Test instance.");
}
public void SayGoodBye()
{
SayHello(); //今日は
Console.WriteLine("さようなら");
}
}
class GenericConstractorDemo
{
static void Main()
{
Test t1 = new Test();
t1.SayHello(); //今日は
t1.SayGoodBye();
}
}
}
using System;
namespace Chapter01All
{
class MyClass
{
public MyClass()
{
Console.WriteLine("Creaing a MyClass instance.");
}
public void SayHello()
{
Console.WriteLine("今日は");
}
}
class Test<T> where T : new()
{
T obj;
public Test()
{
Console.WriteLine("Creating a Test instance.");
obj = new T();
}
public void SayGoodBye()
{
Console.WriteLine("さようなら");
//SayHelloを呼び出せない。継承であれば、出来た。
}
}
class GenericConstractorDemo
{
static void Main()
{
Test<MyClass> t1 = new Test<MyClass>();
t1.SayGoodBye(); //MyClassのSayHelloを呼び出せない。
MyClass objA = new MyClass();
objA.SayHello(); //MyClassのインスタンスは出来る。
}
}
}
using System;
namespace Chapter01All
{
class A
{
string str;
public A(string a = "defalt Class A")
{
str = a;
}
public void OutMsgA()
{
Console.WriteLine(str);
}
}
class B : A
{
string str;
public B(string b)
{
str = b;
}
public void OutMsgB()
{
Console.WriteLine(str);
}
}
class MyGenClass<T, V> where V : T
{
A objT;
B objV;
public void OutMsgAB(string ab)
{
objT = new A(ab);
objT.OutMsgA(); //Class AB
objV = new B(ab);
objV.OutMsgB(); //Class AB
}
}
class Demo
{
static void Main()
{
A objA = new A("Class A");
objA.OutMsgA(); //Class A
B objB = new B("Class B");
objB.OutMsgA(); //Defalt Class A
objB.OutMsgB(); //Class B
MyGenClass<A, B> x = new MyGenClass<A, B>();
x.OutMsgAB("Class AB");
}
}
}