介紹:泛型介紹,索引,Foreach遍歷的解釋,yield方法,path文件操作,Directory類基本操作<目錄>
一:泛型
百度資料:泛型是 2.0 版 C# 語言和公共語言運行庫 (CLR) 中的一個新功能。泛型將類型參數的概念引入 .NET Framework,類型參數使得設計如下類和方法成為可能:這些類和方法將一個或多個類型的指定推遲到客戶端代碼聲明並實例化該類或方法的時候。例如,通過使用泛型類型參數 T,您可以編寫其他客戶端代碼能夠使用的單個類,而不致引入運行時強制轉換或裝箱操作的成本或風險,
使用泛型類型可以最大限度地重用代碼、保護類型的安全以及提高性能。泛型最常見的用途是創建集合類。.NET Framework 類庫在 System.Collections.Generic 命名空間中包含幾個新的泛型集合類。應盡可能地使用這些類來代替普通的類,如 System.Collections 命名空間中的 ArrayList。您可以創建自己的泛型接口、泛型類、泛型方法、泛型事件和泛型委托。可以對泛型類進行約束以訪問特定數據類型的方法。關於泛型數據類型中使用的類型的信息可在運行時通過反射獲取。
* 泛型的好處:代碼的重用。通過使用<T>來進行重用。可以往裡面傳遞不同的參數類型,我們也可以將其定義為泛型方法,泛型類,泛型接口。
* 自定義泛型
裡面有泛型類,泛型方法,泛型接口。
eg 1:
/// 自定義泛型類
public class MyClass1<T>
{
public void SayHi(T teg)
{
Console.WriteLine(teg);
}
}
//泛型方法
public class Myclass2
{
public void Say<T>(T er)
{
Console.WriteLine(er);
}
}
//泛型接口
public interface IMyClass3<T>
{
T Sayhi(); //返回值類型
void SayHello(T msg); //參數類型
}
//實現泛型接口的兩種情況
//1:普通類實現泛型接口
public class Class2 : IMyClass3<String>
{
public void SayHello(string msg)
{
Console.WriteLine(msg);
}
public string Sayhi()
{
throw new NotImplementedException();
}
}
//2:泛型類實現泛型接口
//這裡的IMyClass3<U>的U是根據類接口中的U來決定的。
public class Class3<U> : IMyClass3<U>
{
public void SayHello(U msg)
{
Console.WriteLine(msg);
}
public U Sayhi()
{
throw new NotImplementedException();
}
}
二:創建一個索引
public class MyClass
{
private string[] _data = new string[5];
/// <summary>
/// 定義一個索引
/// </summary>
public string this[int index]
{
get { return _data[index]; }
set { _data[index] = value; }
}
}
創建泛型索引
public class MyClass<T>
{
private T[] _data = new T[5];
/// <summary>
/// 定義一個索引
/// </summary>
///
public T this[int index]
{
get { return _data[index]; }
set { _data[index] = value; }
}
}
三:泛型約束
就是在泛型的後面加上where T:struct/class等。說明泛型以後會在定義的這個約束中來進行T的更換。
var p = new Person();
var etor = p.GetEnumerator();
while(etor.MoveNext())
{
Console.WriteLine(etor.Current.ToString());
}
Console.WriteLine("OK");
Console.ReadKey();
具體類的實現
public class Person:IEnumerable
{
private string [] friends=new string[]{"11","22","33"};
public string Name { get; set; }
public int Age { get; set; }
public IEnumerator GetEnumerator()
{
return new PersonEnumerator(this.friends); //這裡返回的是Object類型
}
}
public class PersonEnumerator:IEnumerator
{
private string[] _friends;
public PersonEnumerator(string[] fs)
{
_friends = fs;
}
private int index = -1; //下標 指代0號前面位
//獲取數據
public object Current
{
get
{
if(index>=0&&index<_friends.Length)
{
return _friends[index]; //通過下標來返回值。
}
else
{
throw new Exception();
}
}
}
//移動下標
public bool MoveNext()
{
if(index+1<_friends.Length)
{
index++;
return true;
}
return false;
}
//將下標還原,初始化
public void Reset()
{
index = -1;
}
}
五:yield方法
yield 關鍵字向編譯器指示它所在的方法是迭代器塊。編譯器生成一個類來實現迭代器塊中表示的行為。在迭代器塊中,yield 關鍵字與 return 關鍵字結合使用,向枚舉器對象提供值。這是一個返回值,例如,在 foreach 語句的每一次循環中返回的值。yield 關鍵字也可與 break 結合使用,表示迭代結束。
六:文件操作
string path = @"F:\VS2008\Projects\ShiPinJiChu__HeiMa\文件操作\bin\Debug\文件操作.exe.config";
//獲取文件名
Console.WriteLine(Path.GetFileName(path));
//獲取文件的後綴
Console.WriteLine(Path.GetExtension(path));
//獲取不帶後綴的文件名
Console.WriteLine(Path.GetFileNameWithoutExtension(path));
//獲取該路徑的目錄部分
Console.WriteLine(Path.GetDirectoryName(path));
//更改文件後綴名
Console.WriteLine(Path.ChangeExtension(path, "dll"));
//合並兩個路徑
string path1 = @"F:\VS2008\Projects\";
string path2 = "xiaohui.exe";
Console.WriteLine(Path.Combine(path1 + path2));
//返回當前用戶的臨時文件夾的路徑。
Console.WriteLine(Path.GetTempPath());
//創建磁盤上唯一命名的零字節的臨時文件並返回該文件的完整路徑。
Console.WriteLine(Path.GetTempFileName());
Console.ReadKey();
* Directory類基本操作<目錄>
//創建目錄
for (int i = 0; i < 10; i++)
{
Directory.CreateDirectory(@"C:\" + i);
}
Console.WriteLine("OK");
Console.ReadKey();
//刪除目錄
for (int i = 0; i < 10; i++)
{
//Directory.Delete(@"C:\" + i); //只能刪除空目錄
Directory.Delete(@"C:\" + i, true);
}
Console.WriteLine("OK");
Console.ReadKey();
//剪切
Directory.Move(@"C:\11", @"C:\222\11");
//重命名 <利用剪切來實現>
Directory.Move(@"C:\222", @"C:\333");
Console.WriteLine("OK");
Console.ReadKey();
//獲取文件下面的所有的子目錄
string path = @"C:\333\11";
var file= Directory.GetFiles(path);
foreach (var item in file)
{
Console.WriteLine(item);
}
//刪除目錄
Directory.Delete(@"C:\333",true);
Console.WriteLine("OK");
Console.ReadKey();