[英]ClassName<Type> or ClassName<Object> for abstract base class
I'm relatively newbie to C# programming. 我是C#编程的新手。 I want to create Abstract Base Class that contains a few of abstract
, and virtual
methods. 我想创建一个包含一些abstract
和virtual
方法的Abstract Base Class 。
By the following two examples, what are their differences of usage and which is more commonly used? 通过以下两个示例,它们在用法上有什么区别,哪个更常用?
Example-1, use Type
keyword. 示例1,使用Type
关键字。
public abstract class DecoratorBase<Type> : ValidatableBindableBase
{
public virtual void SetFields(Type T) { }
public virtual void SetFieldsBack(ref Type T) { }
}
Example-2, use Object
keyword. 示例2,使用Object
关键字。
public abstract class DecoratorBase<Object> : ValidatableBindableBase
{
public virtual void SetFields(Object T) { }
public virtual void SetFieldsBack(ref Object T) { }
}
In my case, Type
or Object
will represent my business model POCO class. 就我而言, Type
或Object
将代表我的业务模型POCO类。
Sorry, if this question sound so naive to you. 抱歉,这个问题对您来说太幼稚了。 Thanks. 谢谢。
Please, don't do that! 拜托, 不要那样做!
It looks like you're a little confused about generics. 您似乎对泛型有些困惑。 When you define a generic abstract type (using <
and >
after the name), you're telling the compiler "I will give you some type, but I don't know which type just now. Use the name I'm giving you as a placeholder for the type later." 定义通用抽象类型时(在名称后使用<
和>
),您会告诉编译器“我会给您一些类型,但我现在不知道是哪种类型。请使用我为您提供的名称稍后用作该类型的占位符。” The convention is to use the upper case T
. 惯例是使用大写T
For example, the following defines a generic abstract class. 例如,以下定义了一个通用抽象类。
public abstract class SomeClass<T> {}
Later, when we create a derived class, we create it with a type: 稍后,当我们创建派生类时,我们使用以下类型创建它:
public class ConcreteClass : SomeClass<int> {}
If we had methods declared in our abstract class that used the type T
, our new ConcreteClass
would replace references to T
with int
. 如果我们在抽象类中声明了使用T
类型的方法,则新的ConcreteClass
将用int
替换对T
引用。 So in your case, you should do: 因此,在您的情况下,您应该执行以下操作:
public abstract class DecoratorBase<T> : ValidatableBindableBase
{
public virtual void SetFields(T param) { }
public virtual void SetFieldsBack(ref T param) { }
}
You can see here, T
represents the unknown type as T
, instead of naming the type Type
and the parameter T
. 您可以在这里看到, T
将未知类型表示为T
,而不是命名类型Type
和参数T
What you were doing is known as 'hiding', that is, When you use Type
or Object
in the way you were doing, it is assigning a new meaning to those words. 什么你做的是被称为“隐藏”,也就是说,当你使用Type
或Object
在你正在做的方式,它被赋予了新的含义,这些话。 You should absolutely avoid hiding in this way. 您应该绝对避免以这种方式隐藏。
Now if you create a concrete class, say: 现在,如果您创建一个具体的类,请说:
public class Decorator : DecoratorBase<Control>
{
public override SetFields(Control param) {}
}
You have type safety: the parameter now has to be a Control
, at least for this concrete implementation. 你有类型安全:该参数现在已经是一个Control
,至少在这个具体实施。 You can define other implementations with different types, and inherit the same base methods. 您可以定义具有不同类型的其他实现,并继承相同的基本方法。
See this question for a simple understanding of how to use generics. 见这个问题对于如何使用泛型一个简单的了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.