ziaur rahman
ziaur rahman

Reputation: 13

Why my class is accessible in program class without mentioning access modifier?

I didn't mention any access modifier in my class and moved my class in a folder; according to the video lecture i'm following the class wouldn't be accessible in my program class but in my case i can access that class; my question is why and how?

Program.cs:

namespace DemoClassApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            var sum = calculator.Add(7,8);
            Console.WriteLine(sum);
        }
    }
}

Calculator.cs

namespapce DemoClassApp2.Math //<- moved the class to a different namespace/folder
{
    class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

Upvotes: 0

Views: 52

Answers (2)

Keith Nicholas
Keith Nicholas

Reputation: 44288

By default a class is internal, which means it can be seen within an assembly. By moving it into a folder, you moved it into a different namespace, but not into a different assembly, so it is still visible.

If you wanted to make it invisible you could add a new project to your solution which is a "class library", if you then move the class into the class library, it won't be visible by your "Program"

Upvotes: 1

Andrew Arthur
Andrew Arthur

Reputation: 1603

The default access modifier for a class is internal, not private.

Upvotes: 0

Related Questions