11.如何获得不区分大小写的子字符串的索引位置
1)通过将两个字符串转换成小写之后使用字符串的IndexOf方法:
string strParent = "The Codeproject site is very informative.";

string strChild = "codeproject";

// The line below will return -1 when expected is 4.
int i = strParent.IndexOf(strChild);

// The line below will return proper index
int j = strParent.ToLower().IndexOf(strChild.ToLower());

2)
using System.Globalization;

string strParent = "The Codeproject site is very informative.";

string strChild = "codeproject";
// We create a object of CompareInfo class for a neutral culture or a culture insensitive object
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;

int i = Compare.IndexOf(strParent,strChild,CompareOptions.IgnoreCase);

二
1. 什么是复制构造函数
我们知道构造函数是用来初始化我们要创建实例的特殊的方法。通常我们要将一个实例赋值给另外一个变量c#只是将引用赋值给了新的变量实质上是对同一个变量的引用,那么我们怎样才可以赋值的同时创建一个全新的变量而不只是对实例引用的赋值呢?我们可以使用复制构造函数。
我们可以为类创造一个只用一个类型为该类型的参数的构造函数,如:
public Student(Student student)
  {
this.name = student.name;
}

使用上面的构造函数我们就可以复制一份新的实例值,而非赋值同一引用的实例了。
class Student
  {
private string name;

public Student(string name)
 {
this.name = name;
}
public Student(Student student)
 {
this.name = student.name;
}

public string Name
 {
get
 {
return name;
}
set
 {
name = value;
}
}
}

class Final

  {

static void Main()

 {

Student student = new Student ("A");

Student NewStudent = new Student (student);

student.Name = "B";

System.Console.WriteLine("The new student's name is {0}", NewStudent.Name);

}

}

The new student's name is A.
2.什么是只读常量
就是静态的只读变量,它通常在静态构造函数中赋值。
class Numbers
  {
public readonly int m;
public static readonly int n;

public Numbers (int x)
 {
m=x;
}

static Numbers ()
 {
n=100;
}

} //其中n就是一个只读的常量,对于该类的所有实例他只有一种值,而m则根据实例不同而不同

. OOPs 一种更优雅的方法是使用System.Globalization命名空间下面的CompareInfo类的IndexOf方法
|