小编Ner*_*ury的帖子

使用私有静态方法的优点

在创建具有内部私有方法的类时,通常为了减少代码重复,不需要使用任何实例字段,将方法声明为静态是否具有性能或内存优势?

例:

foreach (XmlElement element in xmlDoc.DocumentElement.SelectNodes("sample"))
{
    string first = GetInnerXml(element, ".//first");
    string second = GetInnerXml(element, ".//second");
    string third = GetInnerXml(element, ".//third");
}
Run Code Online (Sandbox Code Playgroud)

...

private static string GetInnerXml(XmlElement element, string nodeName)
{
    return GetInnerXml(element, nodeName, null);
}

private static string GetInnerXml(XmlElement element, string nodeName, string defaultValue)
{
    XmlNode node = element.SelectSingleNode(nodeName);
    return node == null ? defaultValue : node.InnerXml;
}
Run Code Online (Sandbox Code Playgroud)

将GetInnerXml()方法声明为静态是否有任何优势?没有意见回复,我有意见.

c# performance

197
推荐指数
6
解决办法
9万
查看次数

为什么有必要在结构上调用:this()来使用c#中的自动属性?

如果我使用如下自动属性在C#中定义结构:

public struct Address
{
    public Address(string line1, string line2, string city, string state, string zip)
    {
        Line1 = line1;
        Line2 = line2;
        City = city;
        State = state;
        Zip = zip;
    }

    public string Line1 { get; protected set; }
    public string Line2 { get; protected set; }
    public string City { get; protected set; }
    public string State { get; protected set; }
    public string Zip { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建文件时,我收到编译错误说The 'this' object cannot be …

c# struct cil automatic-properties

53
推荐指数
1
解决办法
5535
查看次数

标签 统计

c# ×2

automatic-properties ×1

cil ×1

performance ×1

struct ×1