【问题标题】:I am getting an error while producing objects生成对象时出现错误
【发布时间】:2021-07-30 00:12:25
【问题描述】:

我正在开发一个具有 N 层架构的项目。在主函数中生成新对象时出错。我无法给出 car1 的功能。整个文本带有红色下划线,我收到以下错误“CS1922:无法使用集合初始化程序初始化类型‘Car’,因为它没有实现‘System.Collections.IEnumerable’”和“CS0747:无效的初始化程序成员声明器”。

using Business.Concrete;
using System;
using Entities.Concrete;
using DataAccess.Concrete.EntityFramework;

static void Main(string[] args)
    {
        
        Car car1 = new Car
        {
            car1.Id = 4,
            car1.BrandId = 1,
            car1.ColourId = 2,
            car1.ModelYear = 1990,
            car1.Name = "King",
            car1.DailyPrice = 150,
            car1.Description = "Best"
        };

        CarManager carManager = new CarManager(new EfCarDal());

        Console.ReadLine();
    }

实体\混凝土\Car.cs

using Core.Entities;
using System;
using System.Collections.Generic;
using System.Text;

namespace Entities.Concrete
{
    public class Car : IEntity
    {
        public int Id { get; set; }
        public int BrandId { get; set; }
        public int ColourId { get; set; }
        public string Name { get; set; }
        public int ModelYear { get; set; }
        public int DailyPrice { get; set; }
        public string Description { get; set; }
    }
}

【问题讨论】:

    标签: c# console-application n-tier-architecture


    【解决方案1】:

    {} 中初始化时不需要car1. 访问权限,如下所示:

            Car car1 = new Car
            {
                Id = 4,
                BrandId = 1,
                ColourId = 2,
                ModelYear = 1990,
                Name = "King",
                DailyPrice = 150,
                Description = "Best"
            };
    

    【讨论】:

    • 我认为它也需要括号 - new Car() { property=value }
    • @Nikki9696 确实如此。这就是我把它们留在那里的原因。
    • 你没有括号 (),你有括号 {}。无论如何,这一切都很好。 =) 也许不需要。不确定。
    • 哦,那些()。是的,当您还使用 {} 时,您不需要它们作为空构造函数。
    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2013-11-22
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多