Private:
Allowing classes to be private to a namespace would achieve no meaningful level of protection. Because private means that the member is only access in the containing class. Since a top-level class has no class containing it; it cannot be private. If you force to create a private class in Namespace, compiler will throw a compile time error "Namespace elements cannot be explicitly declared as private, protected or protected internal" .
We can declare a class as Private inside other class:
public class Class1
{
temp _temp ;
public Class1()
{
_temp = new temp();
}
public void SetTempClass(string p_str, int p_Int)
{
_temp.setVar(p_str, p_Int);
}
public string GetTempClassStr()
{
return _temp.GetStr();
}
public int GetTempClassInt()
{
return _temp.GetInt();
}
private class temp
{
string str;
int i;
public void setVar(string p_str, int p_int)
{
str = p_str;
i = p_int;
}
public string GetStr()
{
return str;
}
public int GetInt()
{
return i;
}
}
}
Protected:
You cannot declare protected classes at the namespace level because the access modifier for outer level classes defines their visibility in relation to other assemblies. The protected visibility is used to indicate 'visible to derived classes'; this makes sense on things inside a class, but normally has no meaning at the class level.
It can be done with nested classes.
public class OuterClass
{
protected class NestedClass
{
}
}
No comments:
Post a Comment