【问题标题】:How to design a String class with MaxLength?如何设计一个带 MaxLength 的 String 类?
【发布时间】:2019-07-16 13:13:26
【问题描述】:

我计划创建自己的字符串类,但它会提供最大长度。 我们称之为“lString”。 我想在我的代码中像“字符串”类一样使用“lString”。但是我可以为它设置一个最大长度。

例如,应该构建这段代码:

// 1- No maxlength provided, so the object will be created.
lString mylString1 = "0123456789";
// 2- maxlength provided, so it will be checked, and then created.
lString mylString2 = new lString("0123456789", 10);
// 3- This time only maxlength provided, so it will be a string object with maxLength.
lString mylString3 = new lString(20);

// At the end, I should be able to use it like a regular strings:
mylString3 = mylString1 + mylString2;

// Below should throw exception at RunTime, because it will be over 20)
mylString3 = mylString1 + mylString2 + mylString1 + mylString2;

【问题讨论】:

  • 然后呢?有用吗?
  • 我认为这是不可能的。您正在重新分配 mylString3 ,因此无论如何这将是一个新值。
  • @AndersKjeldsen 我重新分配 mylString3 就像您可以分配普通字符串一样。我是具有新价值的 k。
  • case1 可以使用隐式转换运算符。情况2是可能的。案例3是....好的。它的重新分配打破了这一点。一旦你分配了myString3 = ......,它就对预先分配的最大值一无所知。您认为var myString3 = new lString(myString1 + myString2, 20) 是一种可接受的解决方法吗?
  • 你不能用它来扩展字符串,字符串是一个密封的类,扩展至少是你能做的或者只是一个接口,从这里做所有的字符串操作:referencesource.microsoft.com/#mscorlib/system/string.cs跨度>

标签: c# string class oop object


【解决方案1】:

实现一个基本的字符串类是相当简单的,它具有与普通字符串之间的隐式运算符:

public class LimitedString
{
    private readonly string value;
    private readonly long maxLength;

    public LimitedString(string value, long maxLength = long.MaxValue)
    {
        if(value != null && value.Length > maxLength)
            throw new InvalidOperationException("Value is longer than max length");
        this.value = value;
        this.maxLength = maxLength;
    }

    public override string ToString()
    {
        return value;
    }

    public override int GetHashCode()
    {
        return value.GetHashCode();
    }

    public override bool Equals(object o)
    {
        if(o is LimitedString)
            return value == ((LimitedString)o).value;
        return false;
    }

    public static implicit operator LimitedString(string str)
    {
        return new LimitedString(str);
    }

    public static implicit operator String(LimitedString ls)
    {
        return ls.value;
    }        
}

然后您的前 2 个案例按预期工作:

LimitedString myString1 = "0123456789";
LimitedString myString2 = new LimitedString("0123456789", 10);

但是,使第三个示例工作的唯一方法是:

LimitedString myString3 = new LimitedString(myString1 + myString2 + myString1 + myString2, 20); // Throws exception

一旦你重新分配了值,你的最大长度规范就会丢失,所以你不能这样做:

LimitedString myString3 = new LimitedString(20); // This is fine - you could have a constructor that just takes the max length.
myString3 = myString1 + myString2 + myString1 + myString2; // but here you're re-assigning.

现场示例:https://rextester.com/KVBBQT19360

【讨论】:

  • 感谢您的好回答。只有一件事需要修复。此 2 行代码应引发异常: LimitedString myString2 = new LimitedString("0123456789", 10); myString2 = "01234567890123456789";你看,myString2 的 maxLength 已经设置为 10。现在我们尝试将其设置为 20 个字符的字符串。我们应该做不到。
  • @HakanErdogan 您没有阅读我的答案的结尾 - 那是不可能的,您将 myString2 重新分配给一个 与设置了 maxlength 的实例完全不同的实例.
  • 谢谢,我认为这是一个工作示例。我将其标记为答案,因为没有其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多