C# 错误 1 无法使用集合初始值初始化类型“char”,原因是它未实现“System.Collections.IEnumerable”
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSy...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace TestIterator1
{
public class Tokens : IEnumerable
{
private string[] elements;
Tokens(string source, char delimiters)
{
elements = source.Split(delimiters);
}
public IEnumerator GetEnumerator()
{
return new TokenEnumerator(this);
}
private class TokenEnumerator : IEnumerator
{
private int position = -1;
private Tokens t;
public TokenEnumerator(Tokens t)
{
this.t = t;
}
public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
return t.elements;
}
}
}
class Program
{
static void Main(string[] args)
{
Tokens f = new Tokens("This is a well-done program.", new char{' '});
foreach (string item in f)
{
Console.WriteLine(item);
}
}
}
}
}
请问怎么解决 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace TestIterator1
{
public class Tokens : IEnumerable
{
private string[] elements;
Tokens(string source, char delimiters)
{
elements = source.Split(delimiters);
}
public IEnumerator GetEnumerator()
{
return new TokenEnumerator(this);
}
private class TokenEnumerator : IEnumerator
{
private int position = -1;
private Tokens t;
public TokenEnumerator(Tokens t)
{
this.t = t;
}
public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
return t.elements;
}
}
}
class Program
{
static void Main(string[] args)
{
Tokens f = new Tokens("This is a well-done program.", new char{' '});
foreach (string item in f)
{
Console.WriteLine(item);
}
}
}
}
}
请问怎么解决 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询