Can I call a constructor from the constructor of another class in C#?

TenSzop

I'm a newbie in C# and would like to know if, having two classes in the same namespace, I can call a constructor of one in a constructor of the other one?

For example:

class Company
{
    // COMPANY DETAILS
    Person owner;
    string name, website;

    Company()
    {
        this.owner = new Person();
    }
}

The above returns "Person.Person()" is inaccessible due to its protection level. Person class looks like this:

class Person
{
    // PERSONAL INFO
    private string name, surname;

    // DEFAULT CONSTRUCTOR
    Person() 
    {
        this.name = "";
        this.surname = "";
    }
}

Is there anything I'm missing here? Shouldn't the constructor be accessible from wherever in the same namespace?

ViRuSTriNiTy

You defined the constructor as private hence you cannot access it.

The compiler even gives you a hint:

error CS0122: 'Person.Person()' is inaccessible due to its protection level

The C# 6.0 specification state for access modifiers:

When a class_member_declaration does not include any access modifiers, private is assumed.

whereas a class_member_declaration is specified as

class_member_declaration
    : ...
    | constructor_declaration
    | ...
    ;

Only default constructors are public by default when the class is not defined as abstract.

Therefore change

Person() { }

to

public Person() { }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Can I call a constructor from another constructor (do constructor chaining) in C++?

From Dev

Can "new" be used inside the constructor of the class to call another constructor in Java?

From Dev

How to call a method from another class with no constructor

From Dev

Can I call a overloaded constructor of the same class in C#?

From Dev

c++ call constructor within another constructor of the same class

From Dev

In Dart, can you call another constructor from a constructor

From Dev

Can I call the constructor and destructor of a class explicity?

From Dev

C++ constructor call from another constructor with array initialisation

From Dev

C# - Call a constructor from another constructor after some calculations

From Dev

Can I call setHasOptionsMenu() from Fragment Constructor?

From Dev

call another constructor from constructor body

From Dev

Why can't I call a class's non-default constructor in another file?

From Java

Call one constructor from another

From Dev

How to call a super constructor from another inherited class?

From Dev

C++ call constructor in class A from class C (inherritence)

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

Is it possible to call the constructor of a class in another class?

From Java

How do I call one constructor from another in Java?

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

C++14 can not call copy constructor or operator= of class inheriting from unique pointer

From Dev

What happens if I remove the super constructor call from class file?

From Dev

What happens if I remove the super constructor call from class file?

From Dev

Call another constructor as global inside a class

From Dev

How can I call async method from constructor?

From Dev

Explicit constructor with all default arguments can't be called from another constructor of the same class

From Dev

Why do I ever need to point the Array constructor from another source and why can't I call toString() directly on array objects?

From Dev

How can I call a constructor in main() c++?

Related Related

  1. 1

    Can I call a constructor from another constructor (do constructor chaining) in C++?

  2. 2

    Can "new" be used inside the constructor of the class to call another constructor in Java?

  3. 3

    How to call a method from another class with no constructor

  4. 4

    Can I call a overloaded constructor of the same class in C#?

  5. 5

    c++ call constructor within another constructor of the same class

  6. 6

    In Dart, can you call another constructor from a constructor

  7. 7

    Can I call the constructor and destructor of a class explicity?

  8. 8

    C++ constructor call from another constructor with array initialisation

  9. 9

    C# - Call a constructor from another constructor after some calculations

  10. 10

    Can I call setHasOptionsMenu() from Fragment Constructor?

  11. 11

    call another constructor from constructor body

  12. 12

    Why can't I call a class's non-default constructor in another file?

  13. 13

    Call one constructor from another

  14. 14

    How to call a super constructor from another inherited class?

  15. 15

    C++ call constructor in class A from class C (inherritence)

  16. 16

    How do I call a derived class method from the base class constructor in C++?

  17. 17

    Is it possible to call the constructor of a class in another class?

  18. 18

    How do I call one constructor from another in Java?

  19. 19

    How can I call the constructor of the derived class when using CRTP?

  20. 20

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  21. 21

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  22. 22

    C++14 can not call copy constructor or operator= of class inheriting from unique pointer

  23. 23

    What happens if I remove the super constructor call from class file?

  24. 24

    What happens if I remove the super constructor call from class file?

  25. 25

    Call another constructor as global inside a class

  26. 26

    How can I call async method from constructor?

  27. 27

    Explicit constructor with all default arguments can't be called from another constructor of the same class

  28. 28

    Why do I ever need to point the Array constructor from another source and why can't I call toString() directly on array objects?

  29. 29

    How can I call a constructor in main() c++?

HotTag

Archive