5

In Java a method that hasn't any visibility keyword is called package private and can be seen in from objects in the same package.

If a method or a field hasn't any visibility keyword in C# is it also package private and if not whats the visibility then?

And how can I make a field package private in C#?

5 Answers5

11

by default, a class is marked as internal if not specified. In this case, only classes from the same assembly can use it.

Actually, this can be overriden by using the InternalsVisibleToAttribute which allow to autorize a specifif other assembly to see internal types

Steve B
  • 36,818
  • 21
  • 101
  • 174
4

I think the equivalent of a package private in c# is an internal so you can see the method or attributes in the same assembly.

By default a class without access modifier specified is internal.

Here's a good summary

bAN
  • 13,375
  • 16
  • 60
  • 93
2

I think that the corresponding visibility access keyword is internal. You can also declare assemblies and mark internal members as accesible for them.

http://msdn.microsoft.com/en-us/library/0tke9fxk(v=vs.80).aspx

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
2

In C#, the default access for methods and fields is private.

C# does not have Java's "package-private", but internal is close; use keyword internal

See this article and this StackOverflow item.

Community
  • 1
  • 1
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
0

By default, a class member is private. The class itself is by default package private. Package private is called "internal"

sthiers
  • 3,489
  • 5
  • 34
  • 47