C# 如何在c中调用公共void#

C# 如何在c中调用公共void#,c#,C#,我刚开始,我有个问题。我怎么称呼公共空间 e、 g 附言:我已经试过了,但它对我不起作用您只需要通过类对象引用调用该方法 // Blah is a namespace namespace Blah { // blah is the class name public class blah { // x, y and z are member variables. int x, y = 2; int z = 0;

我刚开始,我有个问题。我怎么称呼公共空间

e、 g


附言:我已经试过了,但它对我不起作用

您只需要通过类对象引用调用该方法

// Blah is a namespace
namespace Blah
{
    // blah is the class name
    public class blah
    {
        // x, y and z are member variables.
        int x, y = 2;   
        int z = 0;

        // SayHiis a public static method. Thus it dont requires the 'blah' object to call this method.
        public static void SayHi()
        {
            Console.WriteLine("Hi: " + y);
        }

        // counting is a public non static method. Thus it requires the 'blah' object to call this method.
        public void counting()
        {
            z = x * y;
        }
    }
}

// CallingMethod is the method where you want to call the counting method of class 'blah'
public void CallingMethod()
{
     // As said above, you need a object to call the 'counting' method.
     blah objBlah = new blah();

     // Now call the non static method using this object.
     objBlah.counting();

     // and here you can directly call the non-static method SayHi like,
     blah.SayHi();
}

如果它是一个非静态类,您需要首先实例化它。 然后要调用它的void方法,只需调用它们()


我的代码在第42行,x和y到底在哪里?您拥有的不是法律代码,因为x和y不属于任何类。此外,方法的名称后面需要有括号。lite实际上只是“InvCounter(…)”。请阅读C#的基础教程。在开始编写程序时,您必须注意任何编程语言的正确语法(就像您在英语考试中注意语法错误一样)。下面是我在上面发布的那些家伙,它们只是我想做的一个例子,与我实际做的事情无关:(
// Blah is a namespace
namespace Blah
{
    // blah is the class name
    public class blah
    {
        // x, y and z are member variables.
        int x, y = 2;   
        int z = 0;

        // SayHiis a public static method. Thus it dont requires the 'blah' object to call this method.
        public static void SayHi()
        {
            Console.WriteLine("Hi: " + y);
        }

        // counting is a public non static method. Thus it requires the 'blah' object to call this method.
        public void counting()
        {
            z = x * y;
        }
    }
}

// CallingMethod is the method where you want to call the counting method of class 'blah'
public void CallingMethod()
{
     // As said above, you need a object to call the 'counting' method.
     blah objBlah = new blah();

     // Now call the non static method using this object.
     objBlah.counting();

     // and here you can directly call the non-static method SayHi like,
     blah.SayHi();
}
public class blahblah
{

    int x, y = 2;   // this sets y as 2, but x is not set 

    public void count()
    {
        Console.WriteLine("Hi: " + y);
    }
    public void counting()
    {
        int z =  x * y;
        Console.WriteLine("Z = {0}", z);

    }

}

class Program
{
    static void Main(string[] args)
    {
        blahblah b = new blahblah(); //instantiate the object
        b.count();  //call a method on the object
        b.counting();

        Console.ReadKey();
    }

}    

// output:
// Hi: 2
// z = 0