C#如何避免在类构造函数中引入许多参数?按类字段对集合排序

C#如何避免在类构造函数中引入许多参数?按类字段对集合排序,c#,.net,class,sorting,constructor,C#,.net,Class,Sorting,Constructor,例如,我有一个简单的班级电影院: public class Cinema { public int Id { get; set; } public string Film_title { get; set; } public int Year { get; set; } public string Country { get; set; } public DateTime Cinema_start_date

例如,我有一个简单的班级电影院:

public class Cinema
    {
        public int Id { get; set; }
        public string Film_title { get; set; }
        public int Year { get; set; }
        public string Country { get; set; }
        public DateTime Cinema_start_date { get; set; }
        public DateTime Cinema_end_date { get; set; }
        public int Duration { get; set; }

        public Cinema(string id, string film_title, string year, string country, string start, string end, string duration)
        {
            this.Id = int.Parse(id);
            this.Film_title = film_title;
            this.Year = int.Parse(year);
            this.Country = country;
            this.Cinema_start_date = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            this.Cinema_end_date = DateTime.ParseExact(end, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            if ( this.Cinema_end_date < this.Cinema_start_date)
            {
                Console.WriteLine("End rent lower than start rent ( Replaced )");
                DateTime tmp = this.Cinema_start_date;
                this.Cinema_start_date = this.Cinema_end_date;
                this.Cinema_end_date = tmp;
            }
            this.Duration = int.Parse(duration);
            
        }
   }
但是,我希望按如下方式创建它,以避免方括号和绑定到索引:

Cinema tmp = new Cinema(words); // something like that
此外,我还想避免出现很多if块:

if(type == "Id" || type == "Year" || type == "Duration")
{
    if (Convert.ToInt32(cinema1.GetValue(cinema_c[i + 1])) < Convert.ToInt32(cinema1.GetValue(cinema_c[i])))
    {
        Cinema tmp = cinema_c[i + 1].deep_copy();
        cinema_c[i + 1] = cinema_c[i].deep_copy();
        cinema_c[i] = tmp.deep_copy();
    }
}
if (type == "Country" || type == "Film_title")
{
    StringComparer comparer = StringComparer.OrdinalIgnoreCase;
    if (comparer.Compare(((cinema1.GetValue(cinema_c[i + 1])).ToString()), ((cinema1.GetValue(cinema_c[i])).ToString())) < 0)
    {
        Cinema tmp = cinema_c[i + 1].deep_copy();
        cinema_c[i + 1] = cinema_c[i].deep_copy();
        cinema_c[i] = tmp.deep_copy();
    }
}
if(type==“Id”| | type==“Year”| | type==“Duration”)
{
if(Convert.ToInt32(cinema1.GetValue(cinema_c[i+1]))
等等。。。
我使用这段代码按照类在集合中的一个属性对其实例进行排序。如果你能提出一个替代方案,我将不胜感激。谢谢。

将您的构造函数更改为:

public class Cinema
{
.....

   public Cinema(string[] fields)
   {
            this.Id = int.Parse(fields[0]);
            this.Film_title = fields[1];
            this.Year = int.Parse(fields[2]);
           .... and so on
   }
}


避免方法和构造函数中出现大量参数的首选方法是创建一个类型,如下所示

public class CinemaInfo
{
    public int Id {get; set;}
    public string FilmTitle {get; set;}
    // more properties ....
}

var info = new CinemaInfo() { Id = 1, FilmTitle = "Tha film", .... };
Cinema cinema = new Cinema(info);

如果你传递了大约6-7个参数-创建一个类型/类

是的,这是一个很好的方法,但是在所有构造中都避免使用方括号。?假设我们有1000个参数)@YuriiSmolii如果你有一个类在构造中需要1000个参数,那么你做错了。@YuriiSmolii,如何在不使用索引或每个属性都有参数的情况下将字符串数组转换为类?我的任务是创建一个不依赖于类字段数量的构造函数。不清楚什么是方法
GetValue
。看起来您发布的排序迭代方法是一个旧的实现,它接收字符串属性数组(cinema_c)。如果您有一个
Cinema
数组,则只需将
KeySelector
lambda传递给
IEnumerable OrderBy
扩展方法即可对其进行排序。看起来您正在尝试反序列化对象的字符串表示形式。有很多用于此目的的库可以为您省去所有的麻烦。您从哪里获取这些字符串值?可能是CSV文件?请使用。另请参见。@adilsondalmeidajr它是内置方法,是的,我将它用于自己的冒泡排序。这看起来像一个。为什么要从类型化数据转换为字符串,再转换为类型化数据?这比仅仅传递正确的数据类型更慢、更不准确?
单词是从哪里来的?您可能需要在那里解决问题,而不是在构造函数中。