What is the default when writing class Test

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!!

If you write private class Test {} you have a private class
If you write public class Test {} you have a public class
If you write protected class Test {} you have a protected class
If you write internal class Test {} you have a internal class

Now to my question
If you just write class Test {}
what access is it then on the class Test meaning what is the default access.

//Tony
 
Hello Tony,

It's private by default

TJ> If you write private class Test {} you have a private class
TJ> If you write public class Test {} you have a public class
TJ> If you write protected class Test {} you have a protected class
TJ> If you write internal class Test {} you have a internal class
TJ> Now to my question
TJ> If you just write class Test {}
TJ> what access is it then on the class Test meaning what is the default
TJ> access.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Its PUBLIC by default. YOU CANNOT CREATE PRIVATE, PROTECTED OR PROTECTED
INTERNAL CLASS IN NAMESPACE.
 
So many answers and all wrong...

Classes can be public or internal.
When you do not specify access modifier, classes are internal by default.

"Classes and structs are declared as internal by default unless the keyword
public is added to the class definition" [1]

[1] Access Modifiers (C# Programming Guide),
http://msdn2.microsoft.com/en-us/library/ms173121.aspx
 
Hello Lebesgue,

certainly, class can't be private in terms of C#, but it can be named private
in terms of framework.
If look at IL code you will see "private" identifier, whether you specify
"internal" or not for class it means "almost" nothing for IL.
"almost" means that if you specify "internal" it has effect on initializing
class local variables - in that case IL dosn't create it's own variables
that mapped to the V_* and uses variable that u specified (it doesn't lost
your variables names)


L> So many answers and all wrong...
L> Classes can be public or internal.
L> When you do not specify access modifier, classes are internal by
L> default.
L> "Classes and structs are declared as internal by default unless the
L> keyword public is added to the class definition" [1]
L>
L> [1] Access Modifiers (C# Programming Guide),
L> http://msdn2.microsoft.com/en-us/library/ms173121.aspx
L>
L> L>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello Altaf,

Everything depends on which angle you are looking at class :)
In terms of IL if u specify internal or nothing classes are private. I meant
that case

AN> Its PUBLIC by default. YOU CANNOT CREATE PRIVATE, PROTECTED OR
AN> PROTECTED INTERNAL CLASS IN NAMESPACE.
AN>
AN> "Michael Nemtsev" wrote:
AN>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello!!

If you write private class Test {} you have a private class
If you write public class Test {} you have a public class
If you write protected class Test {} you have a protected class
If you write internal class Test {} you have a internal class

Now to my question
If you just write class Test {}
what access is it then on the class Test meaning what is the default access.

Acces modifiers in c# are by default the most restrictive.

The most restrictive modifier for elements inside a namespace is
internal. The most restrictive modifier for elements inside a class is
private.


class A //internal (Declared inside root namespace)
{
class B //private (Declared inside class a)
{
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top