【发布时间】:2011-05-26 11:38:33
【问题描述】:
我是编程新手,自学,昨天我正在开发一个使用 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,不是吗?
-
首先,看起来它们应该是静态方法 - 我看不到 ctor 或实例变量...