C#不区分大小写的字符串

C#不区分大小写的字符串,c#,string,case-insensitive,C#,String,Case Insensitive,考虑到以下类别 -我可以做些什么来实现不区分大小写的字符串吗? public class Attibute { // The Name should be case-insensitive public string Name { get; set; } public Attibute() { } } public class ClassWithAttributes { private List&

考虑到以下类别
-我可以做些什么来实现不区分大小写的字符串吗?

public class Attibute
{
    // The Name should be case-insensitive
    public string Name
    {
        get;
        set;
    }

    public Attibute()
    {
    }
}

public class ClassWithAttributes
{
    private List<Attributes> _attributes;

    public ClassWithAttributes(){}

    public AddAttribute(Attribute attribute)
    {
        // Whats the best way to implement the check?
        _attributes.add(attribute);
    }
}
public class Attibute
{
//名称应不区分大小写
公共字符串名
{
收到
设置
}
公共阁楼()
{
}
}
带有属性的公共类
{
私人名单


我对该类进行了编辑,使其更加客观和具体

您不能具有不区分大小写的属性,只能具有不区分大小写的操作,如比较。如果有人访问XHTMLonedDelementAttibute.Name,他们将返回一个字符串,该字符串的大小写与创建该字符串的大小写相同


无论何时使用.Name,都可以以忽略字符串大小写的方式实现该方法。

这取决于您试图对字符串执行的操作

如果要比较字符串而不考虑大小写,请使用
StringComparison.OrdinalIgnoreCase
调用
String.Equals
。 如果要将它们放入字典中,请使用字典的比较器
StringComparer.OrdinalIgnoreCase

因此,您可以创建如下函数:

public class XHtmlOneDTDElementAttibute : ElementRegion {
    public bool IsTag(string tag) {
        return Name.Equals(tag, StringComparison.OrdinalIgnoreCase);
    }

    // The Name should be case-insensitive
    public string Name { get; set; }

    // The Value should be case-sensitive
    public string Value { get; set; }
}

如果你想要一个更具体的解决方案,请告诉我你在使用
名称
属性

好吧,在浏览了规范之后,我的看法是,你不需要做任何事情就可以使字符串属性不区分大小写;对它们的操作(如搜索和排序)是无效的


(我知道W3C的HTML建议基本上是这样说的。它的措辞很糟糕。)

或者,您可能希望将属性设置为始终大写,如下所示

public class XHtmlOneDTDElementAttibute : ElementRegion {
    string name;

    // The Name should be case-insensitive
    public string Name {
        get { return name; }
        set { name = value.ToUpperInvariant(); }
    }

    // The Value should be case-sensitive
    public string Value { get; set; }
}

在回答重组后的问题时,您可以这样做:

public class Attribute { public string Name { get; set; } }

public class AttributeCollection : KeyedCollection<string, Attribute> {
    public AttributeCollection() : base(StringComparer.OrdinalIgnoreCase) { }
    protected override string GetKeyForItem(Attribute item) { return item.Name; }
}

public class ClassWithAttributes {
    private AttributeCollection _attributes;

    public void AddAttribute(Attribute attribute) {
        _attributes.Add(attribute);    
        //KeyedCollection will throw an exception
        //if there is already an attribute with 
        //the same (case insensitive) name.
    }
}
public类属性{public string Name{get;set;}
公共类AttributeCollection:KeyedCollection{
public AttributeCollection():base(StringComparer.OrdinalIgnoreCase){}
受保护的重写字符串GetKeyForItem(属性项){return item.Name;}
}
带有属性的公共类{
私有属性集合属性;
公共void AddAttribute(属性){
_属性。添加(属性);
//KeyedCollection将引发异常
//如果已存在具有的属性
//相同(不区分大小写)的名称。
}
}

如果使用此选项,则应使
Attribute.Name
为只读,或在更改时调用ChangeKeyForItem。

NET Framework提供了各种工具来比较不区分大小写的字符串。它们有什么问题?问题是每次使用时都必须记住使用不区分大小写的比较u引用字符串。最好将“不区分大小写”作为属性/类型本身的固有特性,以便默认比较正常工作。因此,要实现这一点,在我的XHTMLONEDDELEment中-我是否能够覆盖字符串比较函数或其他内容?是的。您可以覆盖Object.Equals方法以及impl校正IComparable和重写CompareTo.divinvi:是的,提供一个自定义字符串比较器,用于比较两个字符串的大写变体。这几乎是一个不区分大小写的比较。如果不够小心,输入可能包含非英语文本,因为某些语言的大写/小写字母很奇怪se规则。正如您所提到的,比较大写变体可能会有问题,但由于字符串是不可变的,因此也会影响性能。最好使用内置的字符串比较方法和CurrentCultureInogoreCase或类似选项。我是否还要在XHTMLonedDelement和XHTMLonedDelementAttitButte之间放置一个XHTMLonedDelementCollectionypo:我可以在XHTMLONEDDELENT和XHTMLONEDDELENTATITIBUTEYES之间放置一个XHTMLONEDDATTIBUTE集合吗?你可以制作一个键集;看看我的最新答案伟大的作品很好的解决方案,而且我还从来没有见过键集:)太好了!