C# 通过方法调用在列表中迭代
我正在列表中输入代码。但是,当我尝试在方法调用中迭代代码时,没有返回任何内容 问题代码在“公共类迭代”中找到。我不知道为什么它不会执行 基本上,我希望有人输入信息。输入信息后,我希望用户通过方法调用遍历列表C# 通过方法调用在列表中迭代,c#,C#,我正在列表中输入代码。但是,当我尝试在方法调用中迭代代码时,没有返回任何内容 问题代码在“公共类迭代”中找到。我不知道为什么它不会执行 基本上,我希望有人输入信息。输入信息后,我希望用户通过方法调用遍历列表 using System; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; using System.Linq; public c
using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Iterating
{
List<String> employees = new List<String>();
public void Test2()
{
//This is where I am trying to iterate//
for (int i = 0; i < employees.Count; i++)
{
Console.WriteLine(employees[i]);
}
}
}
public class Testing
{
public void Test1()
{
while (true)
{
List<String> employees = new List<String>();
Regex regex = new Regex(@"((.{5})-\d{2,5}-\d{2,5})|(@.*.com)");
Console.WriteLine("Please enter an e-mail");
string input = Console.ReadLine();
if(string.Equals(input, "quit", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("You have quit the program");
break;
}
else if (match.Success)
{
Console.WriteLine(match.Value);
employees.Add(match.Value);
}
}
}
}
public class Program
{
public static void Main()
{
Testing T1 = new Testing();
T1.Test1();
Iterating I1 = new Iterating();
I1.Test2();
}
}
使用系统;
使用System.Text.RegularExpressions;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
公共类迭代
{
列出员工=新列表();
公共无效测试2()
{
//这就是我试图迭代的地方//
对于(int i=0;i
自
employees
列表员工
列表-他们各自有自己的独立列表。如果希望迭代类打印列表的内容,则需要将有问题的列表传递给它李> 部分示例:
public class Program {
public static void Main() {
List<String> employees = new List<String>();
Testing T1 = new Testing();
T1.Test1(employees);
Iterating I1 = new Iterating();
I1.Test2(employees);
}
}
公共类程序{
公共静态void Main(){
列出员工=新列表();
测试T1=新测试();
T1.测试1(员工);
迭代I1=新的迭代();
I1.测试2(员工);
}
}
您可以修改测试方法以使用已通过的列表,而不是创建新的列表
公共类迭代{
列出员工=新列表();
公共无效测试2()
{
//这就是我试图迭代的地方//
对于(int i=0;i
employees始终为空,因为这与测试无关。employees您可能希望删除它并将列表传递到Test2或构造函数中
此外,员工应在外观看
你可以像这样重新设计你的类
public class Iterating
{
public void Test2(List<String> employees)
{
//This is where I am trying to iterate//
for (int i = 0; i < employees.Count; i++)
{
Console.WriteLine(employees[i]);
}
}
}
public class Testing
{
public List<String> Test1()
{
List<String> employees = new List<String>();//this should be outside the while loop
while (true)
{
Regex regex = new Regex(@"((.{5})-\d{2,5}-\d{2,5})|(@.*.com)");
Console.WriteLine("Please enter an e-mail");
string input = Console.ReadLine();
if(string.Equals(input, "quit", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("You have quit the program");
break;
}
else if (match.Success)
{
Console.WriteLine(match.Value);//where is the match var
employees.Add(match.Value);//where is the match var
}
}
return employees;
}
}
public class Program
{
public static void Main()
{
Testing T1 = new Testing();
var employees = T1.Test1();
Iterating I1 = new Iterating();
I1.Test2(employees);
}
}
公共类迭代
{
公共作废测试2(列出员工)
{
//这就是我试图迭代的地方//
对于(int i=0;i
我编辑了您发布的代码以使其可读,但此代码无法编译。我不确定你所说的“没有返回任何内容”是什么意思,因为这段代码是不会运行的,因为它不会编译。要回答我认为您的问题,您可能需要找到一种方法在两个类/方法之间共享员工列表。这两个列表位于不同的类/对象中。您需要将员工从T1复制到I1您还可以提到,“未返回任何内容”的答案是,该方法是无效的方法,因此没有返回值。经常会出现这样的情况,即使用“未返回任何内容”来代替“不做任何事情”。这里(对我来说)很清楚,OP期望控制台反馈,但什么都没有得到“回到”它。
public class Iterating
{
public void Test2(List<String> employees)
{
//This is where I am trying to iterate//
for (int i = 0; i < employees.Count; i++)
{
Console.WriteLine(employees[i]);
}
}
}
public class Testing
{
public List<String> Test1()
{
List<String> employees = new List<String>();//this should be outside the while loop
while (true)
{
Regex regex = new Regex(@"((.{5})-\d{2,5}-\d{2,5})|(@.*.com)");
Console.WriteLine("Please enter an e-mail");
string input = Console.ReadLine();
if(string.Equals(input, "quit", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("You have quit the program");
break;
}
else if (match.Success)
{
Console.WriteLine(match.Value);//where is the match var
employees.Add(match.Value);//where is the match var
}
}
return employees;
}
}
public class Program
{
public static void Main()
{
Testing T1 = new Testing();
var employees = T1.Test1();
Iterating I1 = new Iterating();
I1.Test2(employees);
}
}