如何设置构造多张记录 [英] How to set mulitple records in constructor

查看:122
本文介绍了如何设置构造多张记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在真实的应用程序中使用这一点,但我是如何做到这一点(C#)只是好奇。

I'm not using this in a real app but I was just curious on how to do this (C#).

我设置的样本数据的一个记录在构造函数:

I set one record of sample data in the constructor :

public class MikesClass
{
    public MikesClass()
    {
     Id = 01; Name = "Mike";      
    }

    public int Id { get; set; }
    public string Name { get; set; }
}

但我对如何设置它的另一个纪录困惑:

but I'm confused on how to set another record in it :

public MikesClass()
        {
         Id = 01; Name = "Mike";  

         Id = 02;  Name = "Tom"; ???

        }

如果能够做到这一点,什么是语法?谢谢

If possible to do this, what is the syntax? thanks

推荐答案

您完全误解的构造是什么。构造函数是用于一个单独的对象。它创建一个单独的对象。因此,你不能设置与它的另一个纪录。该记录将是一个不同的对象。你刚才设置的值作为参数,当你创造另一个纪录构造函数。

You completely misunderstood what a constructor is. A constructor is for one single object. It creates one single object. Thus you cannot set another record with it. That record will be a different object. You just set the values as arguments to constructor when you create another record.

那么,至少应该是这样的 -

So, should at least be like this -

public class MikesClass
{
    public MikesClass(int id, string name)
    {
        Id = id; 
        Name = name;      
    }

    public int Id { get; set; }
    public string Name { get; set; }
}

和在一定距离的地方创造多个记录/对象时 -

and in some distance place when creating multiple records/objects -

var m1 = new MikesClass(0,"name1");
var m2 = new MikesClass(1, "name2");

这篇关于如何设置构造多张记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆