我尝试IEnumerator通过单击按钮调用函数,这样我就可以返回一些内容,但我无法从检查器的On Click()下拉菜单中选择该函数。
我尝试IEnumerator从另一个函数调用 并将该函数分配给按钮,但不起作用!
显然我不能yield return在void函数中执行此操作。
那么有人可以好心地教我该怎么做吗?
非常感激!
如果这个问题听起来很愚蠢,我是新来的枚举集合,请原谅.
我上课了
public class PersonStuff : IPersonStuff, IEnumerable<User>
{
Person person = new Person();
...
IEnumerator<Person> IEnumerable<Person>.GetEnumerator()
{
return (person as IEnumerable<Person>).GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在上面实现了两个接口:一个是IPersonStuff,另一个是IEnumerable.
Person类是一个简单的getter/setter,每个类型都是字符串,例如名称,地址除了dateofbirth是DateTime.
在我的IPersonStuff中我有这个:
interface IPersonStuff
{
...
IEnumerator<Person> IEnumerable<Person>.GetEnumerator();
}
Run Code Online (Sandbox Code Playgroud)
一旦工作,我想以下面的方式(从任何类)来调用它:
IPersonStuff personStuff = new PersonStuff();
foreach (Person u in personStuff)
{
//this loop should iterate thru the elements.
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
基本上我想知道如何通过IPersonStuff接口调用我在PersonStuff类中的IEnumerator IEnumerable.GetEnumerator().
我可以假设IEnumerator我从一个IList(通过GetEnumerator从IEnumerable界面调用方法)得到的将按照列表的顺序给我项目?
我有一个名单Dars
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PantaRei.Components
{
public class Dars
{
public float OpBal { get; set; }
public float PlNeOrRec { get; set; }
public float MiCaCol { get; set; }
public float MiDeCol { get; set; }
public float MiWrOff { get; set; }
public float ClBal { get; set; }
public float Tot
{
get { return OpBal+ PlNeOrRec+ MiCaCol + MiDeCol + MiWrOff; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我创造了一个新的dars
Dars secA1 = …Run Code Online (Sandbox Code Playgroud) 我有一个这样的结构,我有一个数组sorular[] soru.
public struct sorular
{
public string resim;
public string soru;
public string seceneka;
public string secenekb;
public string secenekc;
public string secenekd;
public int dogrucevap;
public int verilencevap;
}
Run Code Online (Sandbox Code Playgroud)
我想使用一个foreach循环,如:
foreach (int i in sorular.dogrucevap)
{
}
Run Code Online (Sandbox Code Playgroud)
但由于cant get enumerator of dogrucevap错误,我不能这样做.我不想使用while或for循环:)
我正在学习C#中的迭代器概念,并且正在试验代码,采用简单的问题并尝试以不同的方式实现.我试图在列表中显示所有术语,因为我正在尝试不同的方法来获得结果.在下面的代码中,我使用了两个类ListIterator和ImplementList.
在ListIterator类中:我定义了一个HashSet,它使用IEnumerator来存储值.这里GetEnumerator()方法返回列表中的值.GetEnumerator在ImplementList类(其他类)中实现.最后,列表显示在控制台中.
public class ListIterator
{
public void DisplayList()
{
HashSet<int> myhashSet = new HashSet<int> { 30, 4, 27, 35, 96, 34};
IEnumerator<int> IE = myhashSet.GetEnumerator();
while (IE.MoveNext())
{
int x = IE.Current;
Console.Write("{0} ", x);
}
Console.WriteLine();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
在ImplementList类中:定义了GetEnumerator(),它使用yield return x返回列表.
public class ImplementList : IList<int>
{
private List<int> Mylist = new List<int>();
public ImplementList() { }
public void Add(int item)
{
Mylist.Add(item);
}
public IEnumerator<int> GetEnumerator()
{
foreach (int x in Mylist)
yield return x;
} …Run Code Online (Sandbox Code Playgroud) 我已经实现了一个自定义链表,我在实现IEnumerator <>时遇到了问题.具体来说,编译器告诉我The name "GetEnumerator" does not exist in the current context.我觉得我正在实现它,正如我在众多stackoverflow帖子和教程中看到的那样,我错过了什么?
这是我的数据结构:
namespace TestReportCreator_v3
{
public class FindingsTable : IEnumerable<string>
{
private Node head, mark;
public class Node
{
public string description; //covers weakness, retinaDesc, nessusDesc
public string riskLevel; //covers impactLevel, retinaRisk, nessusRisk
public string boxName; //box name results apply to
public string scanner; //wassp, secscn, retina, nessus
public string controlNumber; //ia control number impacted, wassp and secscn only
public string fixAction; //comments, retinaFix, nessusSolu
public string auditID; …Run Code Online (Sandbox Code Playgroud) 这是我一段时间以来最烦人的错误.我想用一个简单的循环将我的相机移动到Unity中的另一个点,使用C#.
我正在"使用System.Collections.Generic",IEnumerator甚至会在我开始输入时显示在建议中,但是一旦我完成,它就会变为红色,并且出现"Assets/Scripts/NerworkManager"错误.cs(190,9):错误CS0246:找不到类型或命名空间名称"IEnumerator".您是否缺少using指令或程序集引用?" 在控制台中,以及编辑器中的"错误CS0103:名称IEnumerator在当前上下文中不存在".
这是我的代码:
IEnumerator LerpCam(Camera c, Vector3 target, float length){
float startTime = Time.time;
while (Time.time < startTime + length) {
c.transform.position = Vector3.Lerp (c.transform.position, target, Time.deltaTime);
}
yield return null;
}
Run Code Online (Sandbox Code Playgroud)
我不知道问题是什么,并且能够毫无问题地使用Generic集合中的其他东西.任何帮助将不胜感激.
我有一个包装对象列表的类.我希望这个类与foreach循环兼容.
我已经看到了几个关于如何在SO上做到这一点的问题,但大多数答案都没有为我编译.
class TrackList
{
private List<Track> tracks = new List<Track>();
}
Run Code Online (Sandbox Code Playgroud)
你会添加什么来使其适用于每个?
到目前为止,我有:
class TrackList : IEnumerable<Track>
{
// ...
public IEnumerator<Track> GetEnumerator()
{
return trackList.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()
{
return this.GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
但它说(翻译错误):
错误1'HP.TrackList'未实现接口成员'System.Collections.IEnumerable.GetEnumerator()'.'HP.TrackList.GetEnumerator()'无法实现'System.Collections.IEnumerable.GetEnumerator()',因为它没有与'System.Collections.IEnumerator'对应的返回类型.XXX\TrackList.cs 11
如果相反我使用:
IEnumerator<Track> IEnumerator<Track>.GetEnumerator()
{
return this.GetEnumerator();
}
Run Code Online (Sandbox Code Playgroud)
给我同样的错误.
我有一个客户类型对象列表,当我遍历该列表时,我希望能够遍历每个客户的属性。然后我想将该属性值打印为字符串。我收到 StackOverFlowException 错误。
让我先说:
提前致谢!
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Reflection.Emit;
using System.Reflection;
using System.Collections;
namespace AtoCLib
{
public class DataAccessLayer
{
public static List<Customer> GetCustomerList(string startChar)
{
string sql = $"SELECT TOP (10) P.LastName, [CustomerID],[PersonID] ,[StoreID] ,[TerritoryID] ,[AccountNumber] FROM [AdventureWorks2017].[Sales].[Customer] C INNER JOIN [Person].[Person] P ON C.CustomerID = P.BusinessEntityID WHERE P.LastName >= '{startChar}'";
List<Customer> CustomerList = new List<Customer>();
try
{
using (var connection = new SqlConnection("Data Source=SHCP-2035;Initial Catalog=AdventureWorks2017;Integrated Security=True"))
{ …Run Code Online (Sandbox Code Playgroud) ienumerator ×11
c# ×10
ienumerable ×5
foreach ×3
interface ×2
list ×2
buttonclick ×1
datagridview ×1
function ×1
generics ×1
linked-list ×1
linq ×1
onclick ×1
properties ×1
winforms ×1
yield-return ×1