C# 类,什么';使用方法的最佳方式是什么?
我是编程新手,自学成才,昨天我在开发一个使用C#处理文件的类,我有一个疑问。。。当你有checkmethod和createmethod时,使用这些方法的最佳方式是什么 是的,我知道,我在这里不清楚,所以这里有一个例子 Files.cs(类) 使用此类方法的最佳和最快方法是什么?因为当我使用CreateFile方法时,我必须检查是否已经有同名的文件 最好的方法是在这个方法中引用另一个方法?像这样,C# 类,什么';使用方法的最佳方式是什么?,c#,oop,class,methods,C#,Oop,Class,Methods,我是编程新手,自学成才,昨天我在开发一个使用C#处理文件的类,我有一个疑问。。。当你有checkmethod和createmethod时,使用这些方法的最佳方式是什么 是的,我知道,我在这里不清楚,所以这里有一个例子 Files.cs(类) 使用此类方法的最佳和最快方法是什么?因为当我使用CreateFile方法时,我必须检查是否已经有同名的文件 最好的方法是在这个方法中引用另一个方法?像这样, namespace Working_with_Files { class Files {
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;
}
}
}
}
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方法中?像这样,
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;
}
}
}
}
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,它应该“检查”文件、内容或其他内容。不检查文件是否存在-->重命名为FileExists不要过早进行优化!第一个是“更清楚”,这是一个主观问题
请重命名函数:如果函数名为CheckFile,它应该“检查”文件、内容或其他内容。不检查文件是否存在-->重命名为FileExists就我个人而言,我采用以下方法: 如果“checking”代码不止一行代码,那么我将其移动到它自己的方法 您还可以执行以下操作:
return File.Exists(path);
在CheckFile方法内部
但是关于性能/速度,不要担心。根据需要编写尽可能多的方法,速度差异非常小
在我看来,代码的可读性比微小的性能更重要。就我个人而言,我采用以下方法: 如果“checking”代码不止一行代码,那么我将其移动到它自己的方法 您还可以执行以下操作:
return File.Exists(path);
在CheckFile方法内部
但是关于性能/速度,不要担心。根据需要编写尽可能多的方法,速度差异非常小
在我看来,代码的可读性比微小的性能更重要。如果您想要最快的方式,那么我认为在第一种情况下,您只能使用CreateFile方法。因为它使用现成的框架File.Exists和File.Create方法。正如大多数开发人员所做的那样——如果框架或语言提供了现成的功能,那么就使用它们,否则如果不满足,那么就将最大限度地组合现有的功能
希望这会有帮助 如果您想要最快的方式,那么我认为您可以在第一种情况下只使用CreateFile方法。因为它使用现成的框架File.Exists和File.Create方法。正如大多数开发人员所做的那样——如果框架或语言提供了现成的功能,那么就使用它们,否则如果不满足,那么就将最大限度地组合现有的功能
希望这会有帮助 假设您的方法需要额外的功能,并且您没有锦上添花 我想你是在问是否在另一个方法中复制一个方法的功能,答案是否定的
“在使用CreateFile方法之前在主程序上使用CheckFile方法”允许您扩展CheckFile方法,而不会使其在功能上偏离CreateFile,这是一种更好的封装。(如果CreateFile总是需要调用CheckFile,也可以让它调用CheckFile)假设您的方法需要额外的功能,并且您没有在锦上添花 我想你是在问是否在另一个方法中复制一个方法的功能,答案是否定的
“在使用CreateFile方法之前在主程序上使用CheckFile方法”允许您扩展CheckFile方法,而不会使其在功能上偏离CreateFile,这是一种更好的封装。(或者,如果CreateFile总是需要调用CheckFile,那么就让它调用CheckFile)不需要创建Files类的实例,因此可以按照前面的建议将所有方法都设置为静态,或者使用我认为更优雅的代码模式:
namespace Working_with_Files
{
public class Files
{
private static Files instance;
public static Files Instance { get { return instance; } }
static Files()
{
instance = new Files();
}
private Files()
{
}
public bool CheckFile(string path)
......no change in rest of code.....
}
}
并调用以下方法:
Files.Instance.CheckFile("myfilehere")
无需创建Files类的实例,因此可以按照前面的建议将所有方法都设置为静态,或者使用我认为更优雅的代码模式:
namespace Working_with_Files
{
public class Files
{
private static Files instance;
public static Files Instance { get { return instance; } }
static Files()
{
instance = new Files();
}
private Files()
{
}
public bool CheckFile(string path)
......no change in rest of code.....
}
}
并调用以下方法:
Files.Instance.CheckFile("myfilehere")
你有理由包装CheckFile,不是吗?首先,看起来它们应该是静态方法-我看不到任何构造函数或实例变量…你有理由包装CheckFile,不是吗?首先,看起来它们应该是静态方法-我看不到任何ctor或实例变量…这更像是一个注释,而不是一个答案,因为它与问题无关。更多的是一般性的提示。这更多的是评论,而不是回答,因为它与问题无关。更多的提示。