C# 在不使用类字段的情况下返回静态数组
我有以下基本类和派生类(为了简洁起见,部分类): 我现在想要的是将C# 在不使用类字段的情况下返回静态数组,c#,C#,我有以下基本类和派生类(为了简洁起见,部分类): 我现在想要的是将newint[]{1,2,3,4}放在getter的return部分。但是,这将在每次调用getter时创建一个新数组 是否可以直接返回某种类型的对象,该对象对于类派生的所有对象都保持不变 大致如下(我知道这是无效的C#): 如果您只想将新int[]{1,2,3,4}部分放入getter,那么这很容易 private static int[] _someArray = null; public override int[] s
newint[]{1,2,3,4}
放在getter的return
部分。但是,这将在每次调用getter时创建一个新数组
是否可以直接返回某种类型的对象,该对象对于类派生的所有对象都保持不变
大致如下(我知道这是无效的C#):
如果您只想将新int[]{1,2,3,4}
部分放入getter,那么这很容易
private static int[] _someArray = null;
public override int[] someArray {
get {
return _someArray ?? (_someArray = new int[] { 1,2,3,4 });
}
}
不过,到时候你就会失去只读功能
更新:仿制药解决方案
或者您可以滥用泛型的功能:
// Of course you still want to use the non-generic class Base
abstract class Base {
public abstract int[] someArray { get; }
}
abstract class Base<T> : Base where T: Base<T> {
// Will be created once for every T, i.e. every derived class.
private static int[] _someArray;
public override int[] someArray {
get { return _someArray ?? (_someArray = CreateArray()); }
}
protected abstract int[] CreateArray();
}
sealed class Derived : Base<Derived> {
protected sealed override int[] CreateArray() {
return new int[] { 1,2,3,4 };
}
}
sealed class DerivedB : Base<DerivedB> {
protected sealed override int[] CreateArray() {
return new int[] { 2,3,4,5 };
}
}
//当然,您仍然希望使用非泛型类基类
抽象类基{
公共抽象int[]someArray{get;}
}
抽象类Base:Base,其中T:Base{
//将为每个T(即每个派生类)创建一次。
私有静态int[]_someArray;
公共覆盖int[]数组{
获取{return _someArray???(_someArray=CreateArray());}
}
受保护的抽象int[]CreateArray();
}
派生的密封类:基{
受保护的密封覆盖int[]CreateArray(){
返回新的int[]{1,2,3,4};
}
}
密封类DerivedB:基础{
受保护的密封覆盖int[]CreateArray(){
返回新的int[]{2,3,4,5};
}
}
请注意,这只适用于一个继承级别,因此我密封了一些东西:)据我所知,这在C#中不起作用。可以在属性/方法中使用字段或创建变量。在VB.NET中,局部变量有Static
关键字(见下文)
您已经说过,您有许多派生类,并且不希望每个派生类都有一个静态字段数组。我建议使用不同的方法。您可以在基类中使用静态字典
,并为每个派生类使用枚举:
abstract class Base
{
public abstract DerivedType Type { get; }
protected static readonly Dictionary<DerivedType, int[]> SomeDict;
static Base()
{
SomeDict = new Dictionary<DerivedType, int[]>();
SomeDict.Add(DerivedType.Type1, new int[] { 1, 2, 3, 4 });
SomeDict.Add(DerivedType.Type2, new int[] { 4, 3, 2, 1 });
SomeDict.Add(DerivedType.Type3, new int[] { 5, 6, 7 });
// ...
}
public static int[] SomeArray(DerivedType type)
{
return SomeDict[type];
}
}
public enum DerivedType
{
Type1, Type2, Type3, Type4, Type5
}
class Derived : Base
{
public override DerivedType Type
{
get { return DerivedType.Type1; }
}
}
\u someArray
对于派生的的每个实例都是相同的
使用该字段有什么问题吗?我可以确认:您每次都想要相同的实例吗?还是每次都需要一个不同的实例?@TimSchmelter主要原因是尽可能少的使用双重代码。从一个基派生出许多,它们都有一个类范围的数组。准确地说:数组包含支持的功能列表,这些功能可能因派生的而有所不同。。。您只是不喜欢静态字段/初始值设定项可见?如果是:为什么?您拥有的代码是直接的、有效的、显而易见的:代码中有好的东西code@MarcGravell我同意。我只是一直试图以这样一种方式编码,它保持可维护性,缩短程序员的时间。而且,这些东西帮助我更好地了解C。这可能会奏效。如果我也将\u someArray
字段放在基类中,那么我的派生类都只有一个getter。他根本不想使用字段。@BartFriederichs你不能将\u someArray放在基类中,因为这样你就不会在每个派生类中有一个数组,而只在基类中有一个数组。如果您希望每个派生类都有静态的内容,您就不能在派生类中声明它。@TimSchmelter,这里的主要问题不是使用字段。主要的问题是代码重用。@TimSchmelter他没有明确指出这一点。是的,这将是我的第一个解决方案,但C#不支持这一点。
// Of course you still want to use the non-generic class Base
abstract class Base {
public abstract int[] someArray { get; }
}
abstract class Base<T> : Base where T: Base<T> {
// Will be created once for every T, i.e. every derived class.
private static int[] _someArray;
public override int[] someArray {
get { return _someArray ?? (_someArray = CreateArray()); }
}
protected abstract int[] CreateArray();
}
sealed class Derived : Base<Derived> {
protected sealed override int[] CreateArray() {
return new int[] { 1,2,3,4 };
}
}
sealed class DerivedB : Base<DerivedB> {
protected sealed override int[] CreateArray() {
return new int[] { 2,3,4,5 };
}
}
abstract class Base
{
public abstract DerivedType Type { get; }
protected static readonly Dictionary<DerivedType, int[]> SomeDict;
static Base()
{
SomeDict = new Dictionary<DerivedType, int[]>();
SomeDict.Add(DerivedType.Type1, new int[] { 1, 2, 3, 4 });
SomeDict.Add(DerivedType.Type2, new int[] { 4, 3, 2, 1 });
SomeDict.Add(DerivedType.Type3, new int[] { 5, 6, 7 });
// ...
}
public static int[] SomeArray(DerivedType type)
{
return SomeDict[type];
}
}
public enum DerivedType
{
Type1, Type2, Type3, Type4, Type5
}
class Derived : Base
{
public override DerivedType Type
{
get { return DerivedType.Type1; }
}
}
MustInherit Class Base
Public MustOverride ReadOnly Property someArray() As Integer()
End Class
Class Derived
Inherits Base
Public Overrides ReadOnly Property someArray() As Integer()
Get
Static _someArray As Int32() = New Integer() {1, 2, 3, 4}
Return _someArray
End Get
End Property
End Class