我是新來的編程,學習我自己,昨天我正在開發一個類來處理使用C#的文件,並且我有疑問......當你有一個checkmethod和一個createmethod時,使用這些方法的最好方法是什麼?類,什麼是使用方法的最佳方式?
是的,我知道,我在這裏並不清楚,所以這裏是一個例子;
Files.cs(類)
namespace Working_with_Files
{
class Files
{
public bool CheckFile(string path)
{
if (File.Exists(path))
{
return true;
}
else
{
return false;
}
}
public bool CreateFile(string path)
{
if (CheckFile(path))
{
return false;
}
else
{
File.Create(path);
return true;
}
}
}
}
什麼是使用這個類的方法最好,最快的方法是什麼?因爲當我使用CreateFile方法時,我必須檢查是否有一個具有相同名稱的文件。
最好的辦法是引用這種方法內的另一種方法?喜歡這個;
namespace Working_with_Files
{
class Files
{
public bool CheckFile(string path)
{
if (File.Exists(path))
{
return true;
}
else
{
return false;
}
}
public bool CreateFile(string path)
{
if (CheckFile(path))
{
return false;
}
else
{
File.Create(path);
return true;
}
}
}
}
最好的方法是使用CreateFile方法內的原生File.Exists?喜歡這個;
namespace Working_with_Files
{
class Files
{
public bool CheckFile(string path)
{
if (File.Exists(path))
{
return true;
}
else
{
return false;
}
}
public bool CreateFile(string path)
{
if (File.Exists(path))
{
return false;
}
else
{
File.Create(path);
return true;
}
}
}
}
或者,最好和最快的方法是在使用CreateFile方法之前在主程序中使用CheckFile方法?
這是我的疑問,對不起,如果我不能說清楚。
你有充分的理由來包裝CheckFile,不是嗎? – DaVinci 2010-12-16 10:56:34
首先,看起來他們應該是靜態方法 - 我沒有看到ctor或實例變量... – RPM1984 2010-12-16 10:58:33