如何使用一些自定义方法创建 class,它只是 List<string>?
How can I create class which is just List<string> with some custom methods?
这似乎是一项微不足道的任务,但我找不到如何去做(也不知道如何称呼它)。
例子。我需要 class state
实际上只是字符串列表。它必须这样工作:
state s = new state();
s.Add("John Madden");
//s[0] == "John Madden"
而且我需要添加到这个 class 一些方法。
你可以继承它:
public class State : List<string>
{}
这样,您就拥有了 List<string>
提供的完整界面,以及您自己的类型名称。然后你就可以去添加你自己的方法了。
如果您想对其进行更多控制,例如要隐藏某些方法或更改它们的工作方式,您还可以创建一个实现 IList<string>
的包装器 class 并将其大部分方法委托给私有 List<string>
实例。例如。像这样:
public class State : IList<string>
{
private List<string> internalList = new List<string>();
public string this[int index]
{
get { return internalList[index]; }
set { internalList[index] = value; }
}
public void Add (string item)
{
internalList.Add(item);
}
// etc. for the other IList<T> members
}
与其继承一个列表,我会选择封装一个(为什么的一个重要原因可以在here中找到)。
如果您需要 State
持有自定义的、状态相关的方法,它们与 List<string>
派生的 class.
无关
public class State
{
public List<string> FooBar { get; set; }
public void Foo() { }
public void Bar() { }
}
尝试从 Collection<String>
继承,这是比使用列表更好的方法:Collection versus List what should you use on your interfaces?
public class State : Collection<string>
List is not designed to be easily extensible by subclassing it; it
is designed to be fast for internal implementations. You'll notice the
methods on it are not virtual and so cannot be overridden, and there
are no hooks into its Add/Insert/Remove operations.
我不知道您的确切要求,但坚持使用 List 并使用扩展方法来实现您的附加功能也许是一种合适的方法。
public static class ListExtensions
{
public static void DoSomethingWithMyStrings (this List<string> list)
{
// Implement your additional functionality here
}
}
// Usage:
using ListExtensions;
var myList = new List<string>();
myList.Add("blah");
myList.DoSomethingWithMystrings ();
这似乎是一项微不足道的任务,但我找不到如何去做(也不知道如何称呼它)。
例子。我需要 class state
实际上只是字符串列表。它必须这样工作:
state s = new state();
s.Add("John Madden");
//s[0] == "John Madden"
而且我需要添加到这个 class 一些方法。
你可以继承它:
public class State : List<string>
{}
这样,您就拥有了 List<string>
提供的完整界面,以及您自己的类型名称。然后你就可以去添加你自己的方法了。
如果您想对其进行更多控制,例如要隐藏某些方法或更改它们的工作方式,您还可以创建一个实现 IList<string>
的包装器 class 并将其大部分方法委托给私有 List<string>
实例。例如。像这样:
public class State : IList<string>
{
private List<string> internalList = new List<string>();
public string this[int index]
{
get { return internalList[index]; }
set { internalList[index] = value; }
}
public void Add (string item)
{
internalList.Add(item);
}
// etc. for the other IList<T> members
}
与其继承一个列表,我会选择封装一个(为什么的一个重要原因可以在here中找到)。
如果您需要 State
持有自定义的、状态相关的方法,它们与 List<string>
派生的 class.
public class State
{
public List<string> FooBar { get; set; }
public void Foo() { }
public void Bar() { }
}
尝试从 Collection<String>
继承,这是比使用列表更好的方法:Collection versus List what should you use on your interfaces?
public class State : Collection<string>
List is not designed to be easily extensible by subclassing it; it is designed to be fast for internal implementations. You'll notice the methods on it are not virtual and so cannot be overridden, and there are no hooks into its Add/Insert/Remove operations.
我不知道您的确切要求,但坚持使用 List 并使用扩展方法来实现您的附加功能也许是一种合适的方法。
public static class ListExtensions
{
public static void DoSomethingWithMyStrings (this List<string> list)
{
// Implement your additional functionality here
}
}
// Usage:
using ListExtensions;
var myList = new List<string>();
myList.Add("blah");
myList.DoSomethingWithMystrings ();