Visual Studio2012 - a class not accessible in unit test [duplicate] - c#

by default is a class:
private ?
internal ?
sealed ?

The default for non-nested types is internal.
The default for nested types is private.
In both cases the default (for classes) is unsealed.
The general rule for all members is that if you don't specify an access modifier, it's as private as it can be. The single exception for this is properties which can make one part (i.e. the getter or the setter) more private than the overall property by specifying an access modifier, e.g.
public string Foo { get; private set; }

internal
see:
http://msdn.microsoft.com/en-us/library/ms173121.aspx

Also, it is not sealed by default. I believe nested classes are private by default.

Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.
Accessibility Levels (C#) on MSDN

Related

In C# can we ever use protected access modifier for an interface?

The default access modifier for an interface is public. However,
in C# can we ever use protected access modifier for an interface?
In this example:
class C
{
protected interface I
{
}
}
the nested type I is protected.
This means that I is visible to all of C and also to all classes that derive from C. For example this could be used to make protected instance methods inside C that contain I in their signatures or return types.
Note: The default accessibility for an interface is not public, like you claim. The default accessibility for a member of an interface is public. That is something else.
The default accessibility for the interface itself depends on what the interface itself is a member of. If the interface is a member of an "outer" class or struct (like my C above), it has the usual default accessibility of class and struct members, which is private. If the interface is a direct member of a namespace (possibly the global namespace), the default accessibility is the usual default for all non-nested types, which is internal.
You can change this acceccibility.
See this page : https://msdn.microsoft.com/en-us/library/ms173121.aspx.
It's say that "Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access."
Be carefull, as you see in this link https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx members are public by default and can't be modified.

Default Constructor with Private accessibility

According To Microsoft Class always contain Default Constructor which is private then how is possible to initiate the Object in another Class.
You are simply misreading that page. For a class without a defined constructor:
class C {}
There exists a default, public constructor, C().
If you explicitly declare a constructor, then that default constructor won't exist. If you declare your constructor like:
class C
{
C()
}
then it will be private, as all class members are private unless an explicit public, internal or protected access modifier is used.
You've missed the most important part of the article:
A private constructor is a special instance constructor. It is
generally used in classes that contain static members only. If a class
has one or more private constructors and no public constructors, other
classes (except nested classes) cannot create instances of this class
The common use case they show in the article talks about using static members only via the said class, like this:
class NLog
{
// Private Constructor:
private NLog() { }
public static double Foo = 3.284;
}
Classes do not have default private constructor, they have a public one (except for abstract classes):
If the class is abstract then the declared accessibility for the
default constructor is protected. Otherwise, the declared
accessibility for the default constructor is public. Thus, the default
constructor is always of the form
"The declaration of the empty constructor prevents the automatic generation of a default constructor" (https://msdn.microsoft.com/en-us/library/kcfb85a6.aspx)
If you declare an empty constructor (whether it is private or not, it doesn't matter), you prevent the generation of the default one (which is always public).

If I declare an internal class, what the default access level of internal members be?

I'm building a DLL with some basic functions. Long story short, I'm making a few static classes for the use by devs. These classes use some other classes that do the dirty work, which I marked as internal because I don't want people to access them.
The question is: If I declare a class as internal, what the access level of his members will be?
I'll have to mark as internal all of its members or they are automatically labeled as internal too?
It's a good 2 hours I'm googling and searching in stackoverflow and I'm struggling to find a clear and straight answer which doesn't include 1000 speculations, technical not-so-probable hypotesis and useless decorations...
MSDN is confusing as usual (never found a clear answer on msdn).
From what I can read here http://msdn.microsoft.com/en-us/library/ms173121.aspx I guess that no matter how you set a class access level, all his members will be private (methods, variables and so on).
Help, I don't know
The question is: If I declare a class as internal, what the access
level of his members will be?
The default will be private. If anything else, then it depends. If they are anything other than public, then the access modifier applies as described on MSDN (e.g. not visible outside of the assembly).
However, in the link you posted, there is one gotcha which applies to non-static classes:
Normally, the accessibility of a member is not greater than the
accessibility of the type that contains it. However, a public member
of an internal class might be accessible from outside the assembly if
the member implements interface methods or overrides virtual methods
that are defined in a public base class.
In relation to the last paragraph, since static classes cannot implement interfaces or inherit other classes then you can rest assured. As long you declare your static class internal, the members will not be available in other assemblies (unless your devs use reflection).
To exemplify how it does work for non-static classes:
Assembly 1
public interface ISomePublicInterface
{
int GetValue();
}
internal class InternalClass : ISomePublicInterface
{
public int GetValue()
{
return 100;
}
}
public static class SomeFactory
{
public static ISomePublicInterface GetInternalInstanceAsInterface()
{
return new InternalClass();
}
}
Assembly 2
ISomePublicInterface val = SomeFactory.GetInternalInstanceAsInterface();
Console.WriteLine(val.GetValue()); //-->> Calls public method in internal class
Console.WriteLine(val.GetType());
Guess what the output is?
Assembly1.InternalClass
So, now you have access to the type outside of the assembly and via reflection someone could call other internal methods (it's not the only way to get it).
From MSDN only
The access level for class members and struct members, including
nested classes and structs, is private by default.
interfaces default to internal access.
Hope this table helps:
Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private
Also check this MSDN
Private unless otherwise stated. However, public will have same result as internal.
If you later promote a class from internal to public, then creating public class objects will become visible, whilst internally scoped methods will stay internal.
You might want to consider behaviour in case your class scope gets updated.
Another stack overflow question.
If u Declare any class as "internal" then its means you can access this class in same assembly. But what kind of access specifier you use for class member is decide they are accessible are not in different class in same assembly.
All the members of internal class would be internal and will be accessible with in the same assembly and will not be outside neither class nor members.
If you want to access class in other assemblies, make the class public and member you don't want to be accessed outside assembly make them internal.

Inherits the access modifiers?

Assume i have a class:
public class Products
{
public string ID { get; set; }
public string Name { get; set; }
public string GetItemName() { ... }
public void SetItemName() { ... }
public string GetItemID() { ... }
public void SetItemID() { ... }
//...
}
It is any way to make all the properties or method inherits the parent class's access modifiers,so i don't have to assign [public] to each of properties/method.
public string Name { get; set; }
public string GetItemName() { ... }
public void SetItemName() { ... }
That's not C# code, that's C++. A language that doesn't support properties, but does support this:
public:
string GetItemName();
void SetItemName();
Which is probably what you are really asking for. No, never make the mistake of comparing C# to C++, it resembles the language only in passing. The accessor keyword must be applied to every member. Good thing is, you'll have a lot less of them. Delete the GetItemName and SetItemName methods, the Name property is all you need.
Here's an old magazine article that might be useful to you, "C++ -> C#: What You Need to Know to Move from C++ to C#"
There is no way to do this with class. It's how C# syntax is defined. If you skip access modifier then the default value is applied, for classes it would be internal modifier and for their members private.
Other default values according to specification are:
Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied.
Namespaces and enumeration members are always public, and no access modifiers can be applied.
Delegates have internal access by default.
Any types declared within a namespace or at the top level of a compilation unit (for example, not within a namespace, class, or struct) are internal by default, but can be made public.
No, the language specs say that if you don't specify the access modifier, a default one will be used:
Members in classes and structs are private by default.
Types (classes, structs, delegates, interfaces and enums) are internal by default, unless they are placed within another type (nested classes) when they default to private..
Interface members, enum members and namespaces have no concept of accessibility modifiers, but can be thought of as always being public.

method without access modifier

Ok this is bugging me.. I know I've read it somewhere and google isn't helping.
What is the accessibility level of a method that does not specify an access modifier?
void Foo()
{
//code
}
I want to say internal but I'm not 100% sure.
The default accessibility for a type is internal, but the default accesibility of that type's members depends on the type.
Generally speaking, members of a class are private by default, where as members of a struct are public by default. This varies by language; default struct access modifiers for C++ are public, where as for C#, they are private.
Assuming this is a C# method, since you have the ".net" tag.
People need to differentiate between "member" accessibility and "class" accessibility.
The default accessibility of class members (including methods) in C# is private. See https://msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.140).aspx
The default accessibility of a class itself is internal.
Yes, internal is the default for classes, but private is the default for members.
For a class: Internal is the default if no access modifier is specified.
For a method: Private is the default if no access modifier is specified.
From The C# Programming Language, Third Edition by Anders Hejlsberg et al, section 10.3.5 ("Class Members - Access Modifiers") on page 434:
A class-member-declaration can have any one of the five possible kinds of declared accessibility (§3.5.1): public, protected internal, protected, internal, or private. Except for the protected internal combination, it is a compile-time error to specify more than one access modifier. When a class-member-declaration does not include any access modifiers, private is assumed. [Emphasis mine]
And then in section 11.2 ("Struct Members") on page 539:
Except for the differences noted in §11.3, the descriptions of class members provided in §10.3 through §10.14 apply to struct members as well.
Section 11.3 does not mention anything about access modifiers, so my reading of this implies that members of a struct without an access modifier are also private by default. This corresponds with what MSDN says and with my own experience.
Oh wait, there's one more thing ....
interface method declarations are of course public by definition. So the following implementation is public, without an explicit access modifier.
public class MyClass : IEqualityComparer<MyClass>
bool IEqualityComparer<MyClass>.Equals(MyClass x , MyClass y) {}
}
hope this clarifies all as per screenshot directly from MSDN
Class methods are private and sealed by default in .NET.
This means the method is only visible within the class and cannot be overridden by the inherited class.

Categories