在数组上使用方法

时间:2018-08-13 08:27:56

标签: c# methods

我对C#还是很陌生,试图创建一个计算数组的程序,但是在使用数组上的方法/属性(Reset,PrintCounters,Increment)时遇到了麻烦。问题从for循环及以下出现。在此先感谢您的帮助。

using System;

namespace CounterTest
{
    public class MainClass
    {

        private static void PrintCounters(Counter[] counters)
        {
            foreach (Counter c in counters)
            {
                string name = "";
                int value = 0;

                Console.WriteLine("{0} is {1}", name, value);
            }
        }
        public static void Main(string[] args)
        {
            Counter[] myCounters = new Counter[3];

            myCounters[0] = new Counter("Counter 1");
            myCounters[1] = new Counter("Counter 2");
            myCounters[2] = myCounters[0];

            for (int i = 0; i < 4; i++)
            {
               Counter.Increment(myCounters[0]);
            }
            for (int i = 0; i < 9; i++)
            {
               Counter.Increment(myCounters[1]);
            }
            Counter.PrintCounters(myCounters);
            Counter.Reset(myCounters[2]);
            Counter.PrintCounters(myCounters);
        }
    }
}

Counter类:

using System;
using System.Collections.Generic;
using System.Text;

namespace CounterTest
{
    public class Counter
    {
        private int _count;
        private string _name;

        public Counter(string name)
        {
            _name = name;
            _count = 0;
        }
        public void Increment()
        {
            _count++;
        }
        public void Reset()
        {
            _count = 0;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public int Value
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
            }
        }
    }
}

5 个答案:

答案 0 :(得分:0)

您调用的方法不是静态方法,因此它们的调用方式如下:

for (int i = 0; i < 4; i++)
{
   myCounters[0].Increment();
}
for (int i = 0; i < 9; i++)
{
   myCounters[1].Increment();
}
MainClass.PrintCounters(myCounters); //this is static
myCounters[2].Reset();
MainClass.PrintCounters(myCounters);

答案 1 :(得分:0)

您使用Counter.Increment(myCounters[0])就像增量是Counter上的扩展方法。

public static class ExtentionCounter
{
    public static void Increment(this Counter cnt)
    {
        cnt.Value++;
    }
} 

使用当前定义时,应使用:

myCounters[0].Increment();

答案 2 :(得分:0)

Counter类型,而不是 instance ,因此Counter.Increment是不正确的调用(Increment不是static方法。

 // Given intance of Counter - myCounters[1] call Increment() method on it
 myCounters[1].Increment(); 

代替

 // Call static method of Counter class - Counter.Increment on instance of Counter
 Counter.Increment(myCounters[1]);

等可能是这样的:

    public static void Main(string[] args)
    {
        Counter[] myCounters = new Counter[3]

        myCounters[0] = new Counter("Counter 1");
        myCounters[1] = new Counter("Counter 2");
        myCounters[2] = myCounters[0];

        for (int i = 0; i < 4; i++)
        {
           // call Increment on myCounters[0] instance
           myCounters[0].Increment();
        }
        for (int i = 0; i < 9; i++)
        {
           // call Increment on myCounters[1] instance
           myCounters[1].Increment(); 
        }            

        // PrintCounters method call
        PrintCounters(myCounters);

        // call Reset on myCounters[2] instance
        myCounters[2].Reset();

        // PrintCounters method call 
        PrintCounters(myCounters);
    }

答案 3 :(得分:0)

您正在调用Counter.Increment,然后提供Counter作为参数。这段代码假设Counter是带有静态方法的静态类,情况并非如此。

在for循环中,您应该使用如下代码:

for (int i = 0; i < 4; i++)
{
    myCounters[0].Increment();
}

答案 4 :(得分:-1)

这是您的代码的解决方案。只需与您进行比较,即可看到更改与** 。 在循环计数器为c的情况下,在PrintCounters上,您需要使用** c.Name和c.Value

调用计数器名称和值。
<table>
    @foreach ($MainOrganisations as $Main)
        <thead>
            <th colspan="2">{{ $Main->address }}</th>
        </thead>
        <thead>
            <th>Name</th>
            <th>Code</th>
        </thead>
        <tbody>
        @foreach($Main->subOrganisations as $Sub)
            <tr>
                <td>{{ $Sub->name }}</td>       
            </tr>
        @endforeach
        </tbody>

    @endforeach
    </table>

此外,由于myCounters是Counters的实例,因此您需要这样调用实例的方法: myCounters [0] .Increment() 其他方法与“重设”相同。 要调用静态方法,您无需实例化,但在您的情况下,您需要引用该类以使用方法PrintCounters,如下所示: MainClass.PrintCounters(myCounters);

还使用关键字this.something更改实例变量。

using System;
using Test;

namespace teste
{
   static class MainClass{

        public static void PrintCounters(Counter[] counters)
        {
            foreach (Counter c in counters)
            {

                **string name = c.Name;**
                **int value = c.Value;**

                Console.WriteLine("{0} is {1}", name, value);
            }
        }
   }
    class Program
    {
        static void Main(string[] args)
        {

            Counter[] myCounters = new Counter[3];

            myCounters[0] = new Counter("Counter 1");
            myCounters[1] = new Counter("Counter 2");
            myCounters[2] = myCounters[0];

            for (int i = 0; i < 4; i++) {

              **myCounters[0].Increment();**
            }
            for (int i = 0; i < 9; i++) {

              **myCounters[1].Increment();**
            }

            **MainClass.PrintCounters(myCounters);**
            **myCounters[2].Reset();**
            **MainClass.PrintCounters(myCounters);**
        }
    }
}

我希望对您有帮助