最近开始学习c#,编了一个程序就发现问题,不可访问 因为它受保护级别限制;
具体如下:
程序:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MathExample.Add(5, 10));
}
}
class MathExample
{
static int Add(int x, int y)
{
return x * y;
}
}
}
编译结果:
ConsoleApplication1.MathExample.Add(int,int)不可访问,因为它受级别限制,找了半天终于找到,原来是要在Add(int,int)前面加一个public,不过不是说c#中public是默认的吗?
改后为:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MathExample.Add(5, 10));
}
}
class MathExample
{
public static int Add(int x, int y)
{
return x * y;
}
}
}