我是一些利基语言的强大开发者,我正在有机地学习C#,所以试着学习什么是最佳实践。
我是否可以获得一些反馈,以获取以下示例代码块的最佳信息,例如我正在构建Shopify集成。
namespace WMShopify
{
// Is it common to have the namespace and class the same?
public class WMShopify
{
// Are there different practices for private/public vars?
public string APIKey { get; set; } // Capital
public string password { get; set; } // lower case
public string secretString { get; set; } // Camel
private string _combinedVar; // Camel/underscore for private
}
// Should these be in a separate *.cs file?
public class WMShopifyOrders
{
// Method capital/lower/camel?
public int getOrderCount()
{
// lower/capital/camel?
int localMemberVar = 0;
return localMemberVar;
}
}
// Should these be in a separate *.cs file?
public class WMShopifyProducts
{
public List<string> getProductList()
{
return new List<string>();
}
}
}
答案 0 :(得分:3)
最佳实践:提出一个每个人都同意并遵循的标准。
判决:写在线内
namespace WMShopify
{
// Is it common to have the namespace and class the same?
//No, namespace should probably be the name of the project itself.
public class WMShopify
//this looks like a configuration class
{
// Are there different practices for private/public vars?
public string APIKey { get; set; } // Capital
public string password { get; set; } // lower case
public string secretString { get; set; } // Camel
private string _combinedVar; // Camel/underscore for private
}
// Should these be in a separate *.cs file?
// I like to separate them because what happens when you have 100 classes, you just scroll forever?
public class WMShopifyOrders
{
// Method capital/lower/camel?
// I prefer capital
public int getOrderCount()
{
// lower/capital/camel? Sure
int localMemberVar = 0;
return localMemberVar;
}
}
// Should these be in a separate *.cs file? Yup
public class WMShopifyProducts
{
public List<string> getProductList()
{
return new List<string>();
}
}
}
答案 1 :(得分:1)
虽然这个问题可能最终会被关闭,但MSDN确实提供了大多数.NET开发人员在某种程度上遵循的非常广泛的指导原则:
https://msdn.microsoft.com/en-us/library/ms229002(v=vs.100).aspx
一些亮点:
XmlReader
,而不是XMLReader
。GetSomething()
,SendSomething()
,而非Something()
)命名方法。Get
或Set
命名属性,例如Things
很好但不是GetThings
IsEnabled
,IsReadOnly
,而非Enabled
)。T
或T(description)
,例如TSource
,TKey
,T1/T2/T3