|
-
Apr 24th, 2007, 10:27 AM
#1
[In general]Default access specifier of class and methods
A VB Book by Francesco Balena tells that it is Friend or Internal as in C# for a class. Methods are private by default.
But the ILDASM of EXE states that class is also private, by default.
But if default access specifier of class is private, then how am I able to create objects of them in other classes, essentially in the same assembly??
Any pointers??
Thank you
-
Apr 24th, 2007, 08:44 PM
#2
Re: [In general]Default access specifier of class and methods
Classes are NOT private by default because, in fact, it's not possible to create a private class UNLESS that class is nested inside another. Private means limited in scope to the type in which its declared. If a class is not declared inside the definition of a type then it cannot be private to that type. So, nested classes are private by default while non-nested classes are internal by default.
Now, having said all that, this talk of default access levels should be meaningless anyway. The default access level for anything is only used if you don't explicitly specify an access level. Given that good coding practice dictates that you ALWAYS explicitly specify the access level for everything it should never be an issue what the default access level is. For instance, this code will adopt the default access level for both classes:
C# Code:
class OuterClass
{
class InnerClass
{
// ...
}
// ...
}
Now, from what you have said OuterClass will be internal and InnerClass will be private. I don't even know for sure because, as I said, it should never matter. If you want those classes to have those access levels then you should explicitly specify it:
C# Code:
internal class OuterClass
{
private class InnerClass
{
// ...
}
// ...
}
Now if someone reads that code, including yourself at some future time, they know that you definitely wanted those access levels and you didn't just forget to specify. If you want a different access level for either or both classes then you should specify that too:
C# Code:
public class OuterClass
{
internal class InnerClass
{
// ...
}
// ...
}
Now OuterClass is accessible outside the assembly while InnerClass is accessible outside OuterClass but not outside the assembly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|