Cast an object into an object from a derived class

前端 未结 4 1468
野的像风
野的像风 2021-01-25 06:15

I have a class Record that works fine:

public class Record 
{
    protected string table;
    protected string idcolumn;
    public Record(string _t         


        
4条回答
  •  走了就别回头了
    2021-01-25 06:59

    As I said in comments

    You can cast a Dog to Animal but not Animal to Dog(unless that animal is Dog). More clearer All Dogs are Animal but not all Animals are Dogs

    Record record = new Record();
    Order order = (Order)record;//wont work since record is some record not an Order
    

    To make it work you've to do something like this

    Record record = new Order();
    

    Then you can cast it like this

    Order order = (Order)record;//works since record is Order
    

提交回复
热议问题