重新学习基础:草稿4
原文发布时间为:2008-11-26 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Collections.Generic;
using System.Text;
delegate void Del(string s);
namespace baidu
{
class Class7
{
//static void Main()
//{
// IndexerClass test = new IndexerClass();
// // Call the indexer to initialize the elements #2 and #5.
// test[2] = 4;
// test[5] = 32;
// for (int i = 0; i <= 10; i++)
// {
// System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
// }
// Console.ReadLine();
//}
//static void Main(string[] args)
//{
// DayCollection week = new DayCollection();
// System.Console.WriteLine(week["Fri"]);
// System.Console.WriteLine(week["Made-up Day"]);
// Console.ReadLine();
//}
//static void Main()
//{
// BaseClass b1 = new BaseClass();
// DerivedClass d1 = new DerivedClass();
// b1.Name = "Mary";
// d1.Name = "John";
// b1.Id = "Mary123";
// d1.Id = "John123"; // The BaseClass.Id property is called.
// System.Console.WriteLine("Base: {0}, {1}", b1.Name, b1.Id);
// System.Console.WriteLine("Derived: {0}, {1}", d1.Name, d1.Id);
// TestDele td = new TestDele();
// Class7 c = new Class7();
// helpDelegate hd1 = new helpDelegate(td.method);
// hd1(888);
// hd1 += c.method2;
// hd1(2);
// Console.WriteLine( hd1.GetInvocationList().Length);
// StartThread();
// int n = 0;
// del d = delegate(){ System.Console.WriteLine("Copy #:{0}", ++n); };
// d();
// // Instatiate the delegate type using an anonymous method:
// Printer p = delegate(string j)
// {
// System.Console.WriteLine(j);
// };
// // Results from the anonymous delegate call:
// p("The delegate using the anonymous method is called.");
// Console.ReadLine();
//}
//public delegate int helpDelegate(int a);
//delegate void del();
//delegate void Printer(string s);
//int method2(int a)
//{
// Console.WriteLine("method2");
// return 2;
//}
//public static void StartThread()
//{
// System.Threading.Thread t1 = new System.Threading.Thread
// (delegate()
// {
// System.Console.Write("Hello, ");
// System.Console.WriteLine("World!");
// });
// t1.Start();
// t1.Abort();
//}
public delegate Mammals HandlerMethod();
public static Mammals FirstHandler()
{
return null;
}
public static Dogs SecondHandler()
{
return null;
}
//static void Main()
//{
// HandlerMethod handler1 = FirstHandler;
// // Covariance allows this delegate.
// HandlerMethod handler2 = SecondHandler;
// Console.ReadLine();
//}
//static void Hello(string s)
//{
// System.Console.WriteLine(" Hello, {0}!", s);
//}
//static void Goodbye(string s)
//{
// System.Console.WriteLine(" Goodbye, {0}!", s);
//}
//static void Main()
//{
// Del a, b, c, d;
// // Create the delegate object a that references
// // the method Hello:
// a = Hello;
// // Create the delegate object b that references
// // the method Goodbye:
// b = Goodbye;
// // The two delegates, a and b, are composed to form c:
// c = a + b;
// // Remove a from the composed delegate, leaving d,
// // which calls only the method Goodbye:
// d = c - a;
// System.Console.WriteLine("Invoking delegate a:");
// a("A");
// System.Console.WriteLine("Invoking delegate b:");
// b("B");
// System.Console.WriteLine("Invoking delegate c:");
// c("C");
// System.Console.WriteLine("Invoking delegate d:");
// d("D");
// // Using Reflection to get information from an Assembly:
// System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");
// System.Console.WriteLine(o.GetName());
// // Using GetType to obtain type information:
// int i = 42;
// System.Type type = i.GetType();
// System.Console.WriteLine(type);
// Console.ReadLine();
//}
static short x = 32767; // Max short value
static short y = 32767;
// Using a checked expression
static int CheckedMethod()
{
int z = 0;
try
{
z = checked((short)(x + y));
}
catch (System.OverflowException e)
{
Console.WriteLine(e.ToString());
}
return z;
}
static void Main()
{
Console.WriteLine("Checked output value is: {0}",
CheckedMethod());
Console.ReadLine();
}
}
class Mammals
{
}
class Dogs : Mammals
{
}
public class TestDele
{
public int method(int a)
{
System.Console.WriteLine(a);
return 8;
}
}
// Indexer on an interface:
public interface ISomeInterface
{
// Indexer declaration:
int this[int index]
{
get;
set;
}
}
// Implementing the interface.
class IndexerClass : ISomeInterface
{
private int[] arr = new int[100];
public int this[int index] // indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
{
return 0;
}
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
arr[index] = value;
}
}
}
}
// Using a string as an indexer value
class DayCollection
{
string[ ] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
// This method finds the day or returns -1
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
// The get accessor returns an integer for a given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}
public class BaseClass
{
private string name = "Name-BaseClass";
private string id = "ID-BaseClass";
public string Name
{
get { return name; }
set { }
}
public string Id
{
get { return id; }
set { }
}
}
public class DerivedClass : BaseClass
{
private string name = "Name-DerivedClass";
private string id = "ID-DerivedClass";
new public string Name
{
get
{
return name;
}
// Using "protected" would make the set accessor not accessible.
set
{
name = value;
}
}
// Using private on the following property hides it in the Main Class.
// Any assignment to the property will use Id in BaseClass.
new private string Id
{
get
{
return id;
}
set
{
id = value;
}
}
}
}