Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make class invisible outside of assembly

Lets say I have class Dog:

public class Dog {
    public String Breed { get; set; }
    public String Color { get; set; }
    ...
}

And a class Animal:

public class Animals {
    public Dog[] Dogs { get; set; }

    public Dog[] GetDogs() {
        ...
        return Dogs;
    }
    ...
}

The above to classes are in my class library and I added it as reference to my project.. Everything works fine but what I want is whoever uses this library should not be able to use class Dog, I mean he should not be able to do something like Dog dog = new Dog();. I tried to make it internal, but then, I must also write internal in Animals class and if I do this I can't use the GetDogs() method.

Any suggestions?

like image 381
biox Avatar asked Feb 02 '26 04:02

biox


1 Answers

Make internal constructor for Dog class. internal Dog(){}. Then only that assembly can create new Dog(), but everyone can use Dog class.

like image 65
nikodz Avatar answered Feb 03 '26 19:02

nikodz