C# and Game(3)Exceptions File Generics Indexer Interfaces Namespaces

本文档提供了C#编程语言的关键概念介绍,包括异常处理、文件系统操作、泛型使用、索引器定义、接口实现等内容,并涵盖了如何创建自定义异常、遍历目录树、使用进度对话框进行文件操作等具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# and Game(3)Exceptions File Generics Indexer Interfaces Namespaces

Exceptions and Exception Handling
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/exceptions/

throw new System.DivideByZeroException();

try{
}
catch(DivideByZeroException e){
}
finally{
}

class CustomException : Exception{
public CustomException(string message){
}
}

Creating and Throwing Exceptions
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/exceptions/creating-and-throwing-exceptions

throw new System.ArgumentException();
throw new System.InvalidOperationException();

File System and the Registry
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/file-system/
static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();

static void Main(){
string[] drives = System.Environment.GetLogicalDrives();
foreach (string dr in drives){
System.IO.DriveInfo di = new System.IO.DriveInfo(dr);
if(!di.IsReady){
Console.WriteLine(“The drive {0} cound not be read”, di.Name);
continue;
}
System.IO.DirectoryInfo rootDir = di.RootDirectory;
WalkDirectoryTree(rootDir);
}

static void WalkDirectoryTree(System.IO.DirectoryInfo root){
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;

try{
files = root.GetFiles(“*.*");
}catch(UnauthorizedAccessException e){
log.Add(e.Message);
}catch(System.IO.DirectoryNotFoundException e){
Console.WriteLine(e.Message);
}

if(files != null){
foreach(System.IO.FileInfo fi in files){
Console.WriteLine(fi.FullName);
}
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs){
WalkDirectoryTree(dirInfo);
}
}
}

}

More File Operation
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/file-system/how-to-create-a-file-or-folder
System.IO.Directory.CreateDirectory(pathString);
String fileName = System.IO.Path.GetRandomFileName();
if(!System.IO.File.Exists(pathString)){
using(System.IO.FileStream fs = System.IO.File.Create(pathString)){
}
}

System.IO.File.Copy(sourceFile, destFile, true);

Progress Dialog Box
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/file-system/how-to-provide-a-progress-dialog-box-for-file-operations

Generics
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/generics/
public class GenericList<T>{
void Add(T input) {}
}

GenericList<int> list1 = new GenericList<int>();
GenericList<string> list2 = new GenericList<string>();
GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();

Indexers
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/indexers/
Customer ArrayList<T> ?
using System;
class SampleCollection<T>{
private T[] arr = new T[100];

public T this[int i]{
get { return arr[i]; }
set { arr[i] = value; }
}
}

static void Main(){
var stringColllection = new SampleCollection<string>();
stringCollection[0] = “hi”;
}

Interfaces
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/interfaces/
interface IEquatable<T>{
bool Equals(T obj);
}

public class Car : IEquatable<Car>{
public string Make {get; set;}
public string Model { get; set; }
public string Year { get; set; }

public bool Equals(Car car){
if (this.Make == car.Make && this.Model == car.Model && this.Year == car.Year) {
return true;
} else {
return false;
}
}
}

Interoperability (Close to Microsoft COM)
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/interop/

Namespaces
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/namespaces/
System.Console.WriteLine(“”);

using System;
Console.WriteLine(“");


References:
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/exceptions/

main menu
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/index

unity
https://unity3d.com/learn/tutorials/s/scripting
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值