English 中文(简体)
为什么我能够凌驾于新财产(C#)的同时?
原标题:Why can t I override and new a Property (C#) at the same time?

https://stackoverflow.com/questions/1833216/why-does-this-work-method-overloading-method-overriding-poly吗?” 问题似乎与你一样,可以采用这种方法。 我想知道的是,在我尝试使用财产时,为什么不工作。

public class Foo
{
    public virtual object Value
    {
        get;
        set;
    }
}

public class Foo<T> : Foo
{
    public override object Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            base.Value = (T)value; //inject type-checking on sets
        }
    }

    public new T Value
    {
        get { return (T)base.Value; }
        set { base.Value = value; }
    }
}

C#4.0RC1的错误信息

Error 1 The type ClassLibrary1.Foo already contains a definition for Value ClassLibrary1Class1.cs 31 22 ClassLibrary1

最佳回答

你用同一名称拥有两个财产。 C#不允许这样做。 唯一的例外是索引编制,因为就索引编制而言,签名按指数类别变更。

不能超负荷使用一种只能因返回类型而有所不同的方法。 单一名称的财产基本上受到同一规则的禁止,因为它是内部的一种方法,没有向进入者提出理由。

问题回答

如果你做以下事情的话,你可以这样做。

public interface IFoo<T>
{
    T Value { get; set; }
}

public class Foo<T> : Foo, IFoo<T>
{
    T IFoo.Value
    {
        get { return (T)base.Value; }
        set { base.Value = value; }
    }
}

唯一一件事是,当你提到时,你必须使用接口类型......。

   IFoo<int> myfoo = new Foo<int>();
   int result = myFoo.Value;    //This will give you the typed version

   Foo<int> myfoo = new Foo<int>();
   int result = myFoo.Value;    //This will give throw an exception

C++/CLI能够做到这一点,即使C#能够发挥作用。 在C++/CLI中,你可以明确说,哪些方法是压倒一切的,因此名字不必相应。

你们可以带着一种灵感。

public interface IFoo
{
    object Value { get; set; }
}

public class Foo<T> : IFoo
{
    object _value = null;
    object IFoo.Value
    {
        get { return _value; }
        set { _value = value; }
    }

    public T Value
    {
        get;
        set;
    }
}

根本问题是,永远无法知道哪一种财产可以使用。

例如,

Foo<int> s = new Foo<int>();

s.Value = "hmmm";

因此应当使用哪些财产? 不动产也来自物品,也符合一般财产版本的限制。

您对同一财产有两种定义:<代码>Value,一种由上下定义,另一种是新定义。 返回类型在方法签名中并不区分特征,即如果签名在返回类型上仅有所不同,则该签名被视为相同。 因此,使用压倒性或新的。

在你看来,你可以使用新的而不是压倒目标。 然而,在使用新办法时,你必须始终认为执行取决于要求执行方法的类型。 页: 1

var foo = new Foo();
var foo_ = new Foo<string>();

foo.Value            // calls the implementation on Foo 
foo_.Value           // calls the implementation on Foo<T>
((Foo) foo).Value    // calls the implementation on Foo




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签