C#如何在通用数组中添加数据 [英] C# how to add data in generic array
问题描述
我有两个类`Employee和Controller,Employee的字段是`name,surname,sallary.in控制器类我有一个Generic数组,它有4个对象,就像这样`
public List<雇员> e =新名单< employee>()
{
新员工(Vazgen,Vazgenyan,3000 $),
new员工(Petros,Petrosyan,3500 $),
新员工(Poghos,Poghosyan,2000 $),
新员工(Karen ,Matevosyan,4000 $),
};
我必须创建名称为Insert_Data的函数,我必须在控制台中输入数据,使用逗号`John,Williams, 5000.我必须将它从逗号中除去,结果将是数组中的3个单词{John,Williams,5000}。有了这些数据,我需要创建一个新的Employee并添加到我的通用数组中。我该怎么办?
Main
控制器c =新控制器();
c.Insert_Data();
当我打电话给Insert_Data函数时,它会问我`请输入新员工的数据
我会写John,Williams ,5000然后它将在Array中添加我的新数据并将显示给我,就像这样`
Vazgen,Vazgenyan ,3000
Petros,Petrosyan,3500
Poghos,Poghosyan,2000
Karen,Matevosyan ,4000
John,Williams,5000
我的尝试:
我试过这样的话`
I have a two classes` Employee and Controller,Employee's fields is` name,surname,sallary.in Controller class I have a Generic array which has 4 Objects, like this`
public List<employee> e = new List<employee>()
{
new Employee("Vazgen","Vazgenyan",3000$),
new Employee("Petros","Petrosyan",3500$),
new Employee("Poghos","Poghosyan",2000$),
new Employee("Karen","Matevosyan",4000$),
};
I must create Function which name is Insert_Data,i must input datas in Console with commas`John,Williams,5000.I must to divide it from commas, the result will be 3 word in array`{ "John", "Williams", "5000"}.With these data I need to create a new Employee and add to my generic array.How can i do it?
Main
Controller c = new Controller();
c.Insert_Data();
when i will call Insert_Data function, it will ask me` Please type new Employee's data
I will write John,Williams,5000 then it will add my new data in Array and will show me,like this`
"Vazgen","Vazgenyan",3000
"Petros","Petrosyan",3500
"Poghos","Poghosyan",2000
"Karen","Matevosyan",4000
"John", "Williams", 5000
What I have tried:
I tried like this`
class Controller
{
public List<employee> e = new List<employee>()
{
new Employee("Vazgen","Vazgenyan",3000),
new Employee("Petros","Petrosyan",3500),
new Employee("Poghos","Poghosyan",2000),
new Employee("Karen","Matevosyan",4000),
};
public void Insert_Data()
{
Console.WriteLine("Please type new Emplyoee's data");
string text = Console.ReadLine();
string[] arr = text.Split(',');
}
}
<pre>class Employee
{
public string name;
public string surname;
public int sallary;
public Employee(string a, string b, int c)
{
this.name = a;
this.surname = b;
this.sallary = c;
}
}
<pre>static void Main(string[] args)
{
Controller c = new Controller();
int x;
while (true)
{
Console.WriteLine("\nBelow are list of commands, type one of them to start working.");
Console.WriteLine("\n1.Insert\n2.Delete\n3.List\n4.Help");
Console.Write("\nWhat do you want to do: ");
x = int.Parse(Console.ReadLine());
Console.WriteLine();
switch (x)
{
case 1:
c.Insert_Data
break;
case 2:
c.Delete_Data
break;
}
}
}
推荐答案
),
新员工(Petros,Petrosyan,3500
),
new Employee("Petros","Petrosyan",3500
),
新员工(Poghos) ,Poghosyan,2000
),
new Employee("Poghos","Poghosyan",2000
,
新员工(Karen,Matevosyan,4000
),
new Employee("Karen","Matevosyan",4000
这篇关于C#如何在通用数组中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!