别人问我一个问题的时候突然发现 string 类似乎实现了索引,也不知道是不是,就自己想像了一下,写了个方法实现.如果并非如此,请大家不要笑话!!!! (方法写的不严谨,只是实现一下)
using
System;
using System.Collections.Generic;
public class MyClass
... {
public static void Main()
...{
myString ms = new myString();
ms.str = "china";
string str = "yangzhou";
Console.WriteLine(str[1]);
Console.WriteLine(str[0]);
Console.WriteLine(str[1]);
Console.WriteLine(str[2]);
Console.WriteLine(str[3]);
Console.WriteLine(str[4]);
Console.ReadLine();
}
}
public class myString
... {
public string str;
public char[] array = new char[256];
public char this[int index]
...{
get
...{
switch(index)
...{
case 0:
array = str.ToCharArray();
return array[0];
case 1:
array = str.ToCharArray();
return array[1];
case 2:
array = str.ToCharArray();
return array[2];
case 3:
array = str.ToCharArray();
return array[3];
default:
array = str.ToCharArray();
return array[4];
}
}
}
}
using System.Collections.Generic;
public class MyClass
... {
public static void Main()
...{
myString ms = new myString();
ms.str = "china";
string str = "yangzhou";
Console.WriteLine(str[1]);
Console.WriteLine(str[0]);
Console.WriteLine(str[1]);
Console.WriteLine(str[2]);
Console.WriteLine(str[3]);
Console.WriteLine(str[4]);
Console.ReadLine();
}
}
public class myString
... {
public string str;
public char[] array = new char[256];
public char this[int index]
...{
get
...{
switch(index)
...{
case 0:
array = str.ToCharArray();
return array[0];
case 1:
array = str.ToCharArray();
return array[1];
case 2:
array = str.ToCharArray();
return array[2];
case 3:
array = str.ToCharArray();
return array[3];
default:
array = str.ToCharArray();
return array[4];
}
}
}
}