I have defined a method in a class and am trying to create an instance of the class and then call the method, except it will not compile. What am I doing wrong?
using System;
class Method
{
int Cube ( int x )
{
return ( x*x*x );
}
}
class MethodTest
{
public static void Main()
{
Method M=new Method();
int y = M.Cube (5);
Console.WriteLine(y);
}
}
the Cube
method is private by default. You need to qualify it as public
.
Cube
method public: public int Cube(int x) { ... }
- IVlad
public int Cube(int x)
works. @IVlad is correct. - Brian Genisio
Your Cube
method has no access modifier, which means it is implicitly private. Change the signature to public int Cube(int x)
and retry.