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

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


你可能感兴趣的:(C# and Game(3)Exceptions File Generics Indexer Interfaces Namespaces)