property VS index
<span style="font-size:18px;"><span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
interface Student
{
int this[int y,int m,int d]{get;set;}
}
class MyCalender
{
private int y, m, d;
public MyCalender()
{
}
public MyCalender(int _y,int _m,int _d)
{
y = _y;
m = _m;
d = _d;
}
public string DayOfWeek
{
get
{
System.DateTime dt = new DateTime(y, m, d);
return dt.DayOfWeek.ToString();
}
}
public bool IsLeapYear
{
get
{
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
return true;
else
return false;
}
}
public string this[int year,int month,int day]
{
get
{
System.DateTime dt = new DateTime(year, month, day);
return dt.DayOfWeek.ToString();
}
}
}
class Program
{
static void Main(string[] args)
{
MyCalender mc = new MyCalender();
//Console.WriteLine(mc.DayOfWeek);
Console.WriteLine(mc[2016, 3, 25]);
Console.ReadLine();
}
}
}
</span>
http://www.cnblogs.com/tank110/archive/2010/10/18/1854013.html
<span style="font-size:18px;">http://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html</span>
范范的作业 : 重载indexer的this。
任务:写一个Population类,一个列表存储城市和人口,可以增删,可以查询,计算总人口,并用重载this的方法来获取人口数排行第n的城市。
<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Population
{
private Hashtable popluation = new Hashtable();
public int this[string index]
{
get
{
return Int32.Parse(popluation[index].ToString());
}
set
{
popluation.Add(index, value);
}
}
public string this[int n]
{
get
{
ArrayList al = new ArrayList(popluation.Values);
al.Sort();
al.Reverse();
int value = Int32.Parse(al[n-1].ToString());
foreach(DictionaryEntry d in popluation)
{
if (Int32.Parse(d.Value.ToString()) == value)
return d.Key.ToString();
}
return null;
}
}
private int totalPopulation;
private int amount;
private string topCity;
public int TotalPopulation
{
get
{
int totalpop = 0;
IDictionaryEnumerator en = popluation.GetEnumerator();
while (en.MoveNext())
{
totalpop += Int32.Parse(en.Value.ToString());
}
return totalpop;
}
}
public int Amount
{
get
{
int count = 0;
IDictionaryEnumerator en = popluation.GetEnumerator();
while (en.MoveNext())
{
count++;
}
return count;
}
}
public string TopCity
{
get
{
string currentTopCity = "";
int currentTopCount = 0;
IDictionaryEnumerator en = popluation.GetEnumerator();
while (en.MoveNext())
{
if (Int32.Parse(en.Value.ToString()) > currentTopCount)
{
currentTopCount = Int32.Parse(en.Value.ToString());
currentTopCity = en.Key.ToString();
}
}
return currentTopCity;
}
}
public void AddCity(string city, int pop)
{
popluation.Add(city, pop);
}
public void RemoveCity(string city)
{
popluation.Remove(city);
}
public void ShowAllData()
{
IDictionaryEnumerator en = popluation.GetEnumerator();
while (en.MoveNext())
{
Console.WriteLine(en.Key.ToString());
Console.WriteLine(en.Value);
}
}
}
class Program
{
static void Main(string[] args)
{
Population population = new Population();
population["shanghai"] = 5000;
population["shenzhen"] = 6000;
//Console.Write(population["shanghai"]);
// population.ShowAllData();
// population.RemoveCity("shanghai");
// population.ShowAllData();
population.AddCity("guangzhou", 70000);
population.ShowAllData();
// Console.WriteLine(population.TotalPopulation);
// Console.WriteLine(population.Amount);
// Console.WriteLine(population.TopCity);
Console.WriteLine(population[2]);
Console.ReadLine();
}
}
}
</span>