Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从父类赋值。容易的_C#_Parent Child - Fatal编程技术网

C# 从父类赋值。容易的

C# 从父类赋值。容易的,c#,parent-child,C#,Parent Child,你是如何做到这一点的: Class Car: string Name, int year Class SUV:Car; SUV bmw=ToCar(car); 像那样的东西 public object ToObject(Type type, Car car) { // here i just wanna take string and int pool } 我做了这样的事情: List<Car> cars=new List<Car> SUV suv=new SUV

你是如何做到这一点的:

Class Car: string Name, int year
Class SUV:Car;

SUV bmw=ToCar(car);
像那样的东西

public object ToObject(Type type, Car car)
{
// here i just wanna take string and int pool
}
我做了这样的事情:

List<Car> cars=new List<Car>

SUV suv=new SUV();

cars.Add(suv).
但我得到一个错误,这是错误的铸造/


我只想恢复SUV。

看起来您已经有了基本的想法,在您的示例中出现了一些命名错误,我假设这些错误只是输入错误,而不是实际代码的样子……类似这样的情况很好,这就是您所了解的:

static void Main(string[] args)
        {
            List<Car> cars = new List<Car>();
            SUV suv = new SUV { name = "mySuv", year = 2010 };
            cars.Add(suv);

            SUV anotherSuvReference = (SUV)cars[0];
            Console.WriteLine(anotherSuvReference.name);
        }

        class Car
        {
            public string name;
            public int year;
        }
        class SUV : Car
        {
        }
static void Main(字符串[]args)
{
列出车辆=新列表();
SUV SUV=新SUV{name=“mySuv”,年份=2010};
汽车。添加(suv);
SUV其他SUV参考=(SUV)汽车[0];
Console.WriteLine(另一个suvreference.name);
}
班车
{
公共字符串名称;
公共国际年;
}
SUV类:轿车
{
}

您的问题不是很清楚。尝试将代码示例重新格式化为c#类。我会自己做,但我不确定
细节
目录
目录
在哪里起作用。
static void Main(string[] args)
        {
            List<Car> cars = new List<Car>();
            SUV suv = new SUV { name = "mySuv", year = 2010 };
            cars.Add(suv);

            SUV anotherSuvReference = (SUV)cars[0];
            Console.WriteLine(anotherSuvReference.name);
        }

        class Car
        {
            public string name;
            public int year;
        }
        class SUV : Car
        {
        }