PrintCounters()
yöntemini kullanarak mycounters[0]
yöntemini yazdırmaya çalışıyorum, ancak PrintCounters(mycounters[0])
çalışmıyor gibi görünüyor, bir sözdizimi sorunu mu? Ben programı hata ayıklama sırasında, orada enter image description herePrintCounters() çağrısı yürütmüyor, neden?
gösterileri belirterek ve bu benim sınıf dosyası olduğu, her şeyi sabit beri
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);
}
}
, hiçbir hata yoktur.
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 'yönteminiz bir' Counter' dizisi gerektirir, tek bir Örnekle çağırmaya çalışıyorsunuz (myCou nter [0] == "mycounters listenizin ilk öğesi): ne yazdırmak istiyorsunuz? tüm liste ya da sadece ilk eleman? –