Unavailable class. VS2010

I really don't know what the problem is with VS2010. I created a class, and when I try to instantiate the class, I get the error: Error xxx not available due to security level.

Example:

public class Person
{
    Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
    public string name;
    public int age;

}

class Program
{
    static void Main(string[] args)
    {

        Person ps = new Person("Jack", 19);
    }
}

      

+2


a source to share


2 answers


Try adding a public key to the Person constructor:



public Person(string name, int age)

      

+6


a source


You need to create your constructor public

:

public Person(string name, int age)
{
    ...

      

Why aren't they public

default constructors, you might ask ? What point of the class you cannot create via your constructor? Well this can be useful if you want to create a class that can only be instantiated using factory methods, eg.



public class Person
{
    public static Person makePerson(string name, int age)
    {
        ...

      

A factory method that is a member of a class Person

may not access a constructor public

.

+6


a source







All Articles