PrintCounters()
メソッドを使用してmycounters[0]
を印刷しようとしていますが、PrintCounters(mycounters[0])
が機能していないようですが、それは構文の問題ですか?私はプログラムをデバッグしていながら、そこまでenter image description herePrintCounters()を呼び出すことは実行されていません、なぜですか?
ショーに注目し、これは私のクラスファイルであるが、すべてのものを固定しているので、
using System;
using System.Collections.Generic;
namespace Counter
{
class MainClass
{
List<Counter> counters = new List<Counter>();
public static void PrintCounters (IEnumerable<Counter> counters)
{
foreach (Counter c in counters)
{
Console.WriteLine("{0} is {1}", c.Name,c.Count);
}
}
public static void Main (string[] args)
{
List<Counter> mycounters = new List<Counter>();
mycounters.Add(new Counter ("counter1"));
mycounters.Add (new Counter ("counter2"));
mycounters [2] = mycounters [0];
for (int i = 0; i < 4; i++) {
mycounters[0].increment();
}
for (int i = 0; i < 9; i++) {
mycounters[1].increment();
}
PrintCounters (mycounters);
mycounters [2].reset();
PrintCounters (mycounters);
}
}
は、エラーがありません。
namespace Counter
{
public class Counter
{
private int _count;
public int Count
{
get{
return _count;
}
}
private string _name;
public string Name
{
get {
return _name;
}
set{
_name = value;
}
}
public Counter (string name)
{
_name = name;
_count = 0;
}
public void increment()
{
_count++;
}
public void reset()
{
_count = 0;
}
}
}
あなたの 'PrintCounters'メソッドは' Counter'の配列を必要とします。あなたはそれをCounter(myCou nter [0] ==あなたの 'mycounters'リストの最初の要素):あなたは何を印刷したいですか?リスト全体か、最初の要素か? –