简体   繁体   English

C#错误CS0266:无法将类型“狗”隐式转换为“ Rottweiler”

[英]C# error CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler

I was trying the following code from a book when the error occured. 发生错误时,我正在尝试从书中的以下代码。 I am geussing it is generated because of wrong use of as keyword. 我在猜测它是由于错误使用as关键字而生成的。 Please, help me fix this error. 请帮我解决此错误。 This code is an example for subclassing. 此代码是子类化的示例。 This code generates two errors (cs0266). 此代码生成两个错误(cs0266)。 The error generating lines are in the Main method and are marked with a comment above the line. 错误生成行位于Main方法中,并在该行上方标有注释。

class Program
{
    static void Main(string[] args)
    {
        // CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler
        Rottweiler butch = new Rottweiler("Butch") as Dog;

        // CS0266: Cannot implicitly convert type 'Dog' to 'Spaniel
        Spaniel mac = new Spaniel("Mac", "yips") as Dog;

        butch.Bark();
        mac.Bark();
        butch.DoYourThing();
        mac.DoYourThing();
    }
}
class Dog
{
    protected string _name;
    protected string _sound;

    public Dog(string name)
    {
        _name = name; _sound = "barks";
    }

    public Dog(string name, string sound)
    {
        _name = name;
        _sound = sound;
    }

    public void Bark()
    {
        Console.WriteLine("{0} {1} at you", _name, _sound);
    }

    public virtual void DoYourThing()
    {
    }
}

class Rottweiler : Dog
{
    public Rottweiler(string name) : base(name) { }
    public Rottweiler(string name, string sound) : base(name, sound) { }

    public override void DoYourThing()
    {
        Console.WriteLine("{0} snarls at you, in a very menacing fashion!", _name);
    }
}
class Spaniel : Dog
{
    public Spaniel(string name) : base(name) { }
    public Spaniel(string name, string sound) : base(name, sound) { }

    public override void DoYourThing()
    {
        Console.WriteLine("{0} drools all over you, then licks you into submission!", _name);
    }

}

While you can cast Spaniel to Dog , you can't do it the other way round. 虽然您可以将Spaniel投放到Dog ,但是却无法做到这一点。 So this code: 所以这段代码:

Spaniel mac = new Spaniel("Mac", "yips") as Dog;

Is casting to Dog and then attempting to store that in a Spaniel variable. 正在强制转换为Dog ,然后尝试将其存储在Spaniel变量中。 You can however do this: 但是,您可以这样做:

Dog mac = new Spaniel("Mac", "yips") as Dog;

Also as mentioned by @leppie, the as Dog cast is not needed as there exists an implicit cast from a derived class to it's base: 就像@leppie提到的那样,由于存在从派生类到其基类的隐式转换,因此不需要as Dog强制转换:

Dog mac = new Spaniel("Mac", "yips");

Well, here: Rottweiler butch = new Rottweiler("Butch") as Dog; 好吧,这里: Rottweiler butch = new Rottweiler("Butch") as Dog; you're creating instance of Rottweiler and casting it to the Dog . 您正在创建Rottweiler实例并将其投放到Dog

Still ok for now, but then you're assigning instance of Dog to the variable of type Rottweiler - but this is impossible. 现在还是可以的,但是您要将Dog实例分配给Rottweiler类型的变量-但这是不可能的。

Since Rottweiler is inherited from Dog , every Rottweiler is Dog , but not every Dog is Rottweiler - thus this implicit cast during assignment can't be done. 由于Rottweiler是从Dog继承的,因此每个Rottweiler都是Dog ,但是并不是每个Dog都是Rottweiler ,因此分配期间无法执行此隐式转换。

So either remove cast to Dog 所以,要么删除投给Dog

Rottweiler butch = new Rottweiler("Butch");

or change type of butch to Dog : 或将butch类型更改为Dog

Dog butch = new Rottweiler("Butch");

Notice in this case you also don't need explicit cast ... as Dog , it will be done implicitly with assignment; 请注意,在这种情况下,您也不需要显式的强制转换... as Dog ,它将通过赋值隐式完成;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误CS0266:无法将类型&#39;object&#39;隐式转换为&#39;int&#39; - error CS0266: Cannot implicitly convert type 'object' to 'int' CS0266 C# 无法将类型“UnityEngine.Object”隐式转换为“”。 存在显式转换(您是否缺少演员表?) - CS0266 C# Cannot implicitly convert type 'UnityEngine.Object' to ''. An explicit conversion exists (are you missing a cast?) C# CosmosDb 查询错误 CS0266:无法隐式转换类型 'System.Linq.IQueryable <system.collections.generic.list<xxxx> ' 到</system.collections.generic.list<xxxx> - C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<xxxx>' to 错误:CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39;。 存在显式转换(您是否缺少演员表?) - Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) 条件运算符。 错误CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39; - Conditional operator. Error CS0266 Cannot implicitly convert type 'int' to 'byte' 错误 CS0266:无法将类型“float”隐式转换为“int”。 存在显式转换(您是否缺少演员表?) - error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) 清单<t> - 编译器错误 CS0266 - 无法隐式转换类型</t> - IList<T> - Compiler Error CS0266 - Cannot implicitly convert type CS0266 无法转换类型 - CS0266 cannot convert type 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) CS0266 无法隐式转换类型 'System.Collections.Generic.IEnumerable<char> '到'字符串'</char> - CS0266 cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM