It might be a silly question, I appreciate if someone can help me understand it.
Can an interface in C# can have static variables?
If the interface itself need to be static to declare static variables inside?
How the implementation goes for static variables(Or say property) within an interface, when we implement in a class?
Some examples and perspicuous explanation would be greatly appreciated.
No, an interface in C# can't declare fields at all. You can't declare a static interface at all in C#, nor can you declare static members within an interface.
As per section 11.2 of the C# specification:
An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind.
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.
An interface is a contract, ie a description of the public instance methods and properties that any implementing class must provide.
Interfaces cannot specify any static methods or properties. They cannot specify internal, protected or private methods or properties. Nor can they specify fields.
1- No, because interface is not a class
2- Consider an Abstract class
3- Static Property in an interface is not defined nor is meaningful in C#
Related
Is there any difference betwen public interface declaration and interface? (I thought that interfaces are public by default).
I am asking because VS2012 is whining about access levels.
I have declared:
interface Ixyz
{nothing important here}
and property (in another class who is using Ixhz as its type):
public Ixhz Somename
{nothing important here}
And when I try to compile the project, it whines about access levels but when I declare interface like public interface Ixyz it stops doing it. Are there any consequences of adding public to interface?
Members in interfaces are always public, and in fact cannot have access modifiers.
Interfaces themselves have the same default access level as other types.
Specifically, top-level types are internal by default, and nested types are private by default.
I want to create a class outside a namespace so that its default access modifier is 'PRIVATE'. I am doing like this:
namespace KnowStructs
{
class Clas1 {}
}
class MyClass {}
But still my class 'MyClass' is referred as Internal when I look in Reflector.
Can anyone help me out.
From Accessibility Levels:
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
and:
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
and for private:
Private members are accessible only within the body of the class or the struct in which they are declared
That is, the private keyword is explicitly defined in terms of a containing class or struct.
So whatever you're trying to do, I don't understand it. How could a top level private type possibly be useful? No other code would be able to reference it (in any way, even if it had e.g. static factory methods).
If a private class is allowed that is not a nested type then what would that mean? If it is more restrictive than internal then how would you use it or create an instance. Any use case will require it to be internal at a minimum. I would like to see how you intend to use it.
It simply makes no logical sense.
Whereas having a private nested class scopes itself to the parent containing class. If it were internal then you still will be able to make an instance within the assembly.
So for classes having no modifier is internal by default for non nested types and private for nested types as .Net always applies the most restrictive access when no modifier is specified.
You can make the class internal, if you only want to be accessible by classes in your namespace
I'm trying to build a C# project of another guy.
In a lot of interfaces I get the error:
The modifier 'abstract' is not valid for this item
In the following Interface:
namespace Services.Email
{
using System;
using System.Collections;
using.System.Collections.Generic;
using System.Runtime.CompilerServices;
public interface IEmailService
{
abstract event EventHandler<SendCompletedEventArgs> SendSummaryCompleted;
void SendNewsItem(DocumentNewsItem newsItem, string email);
void SendSummaryAsync(Session session, Advisor advisor);
}
}
Just remove abstract, it's not applicable to interfaces. Everything in an interface is already essentially "abstract". An abstract class is actually in many ways the same thing as a class with a required interface that is not implemented.
Refer to this MSDN article: http://msdn.microsoft.com/en-us/library/aa664580%28v=vs.71%29.aspx
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.
Solution: Remove "abstract" modifier
Interfaces are not allowed to contain modifiers like abstract, virtual, public, protected, private, ...
Solution:
Just remove it.
In .NET and C#, member modifiers in interfaces aren't supported.
If you want such thing you'd be better switching them to abstract classes, but IMHO this isn't a good way of developing software (refactoring code without thinking what's the actual requirement).
Easy solution: just remove any modifier, leave type, identifier and parameters of any kind of interface member.
It is indeed not valid. Remove the abstract keyword and re-compile.
Are abstract methods internally public and virtual in c#?
All methods are, by default, private and if an abstract method is private, it will not be available to derived class, yielding the error "virtual or abstract members cannot be private"
I think you are asking a different question than most people think (in other words it seems like you understand what abstract means).
You cannot declare a private abstract method - the compiler issues an error. Both of these classes will not compile:
class Foo
{
private abstract void Bar();
}
class Baz
{
// This one is implicitly private - just like any other
// method declared without an access modifier
abstract void Bah();
}
The compiler is preventing you from declaring a useless method since a private abstract member cannot be used in a derived class and has no implementation (and therefore no use) to the declaring class.
It is important to note that the default access modifier applied to an abstract member by the compiler (if you do not specify one yourself) is still private just like it would be if the method was not abstract.
Abstract is just a way to say: "I am here, but no one has told me what I'm going to do yet." And since no one has implemented that member yet someone must do that. To do that you have to inherit that class, and override that member.
To be able to override something it has to be declared either abstract or virtual, and must at least be accessible to the inheritor, i.e. must be marked protected, internal or public.
Abstract methods cannot be private and are virtual. They must be at least protected.
By virtue of Jon Skeet's argument here (What are the Default Access Modifiers in C#?)
The default access for everything in C# is "the most restricted access you could declare for that member"
It must be "protected"
As pointed out by Pieter default is always private, so:
abstract class Foo
{
abstract void Bar();
}
Gives compiler error
virtual or abstract members cannot be private
I'm trying to find a reference for the default visibility of various aspects of C#. Class types, fields, methods, enums, etc.
Can someone provide a list of these along with their default visibility (i.e., no prefixed modifier)?
All of the information you are looking for can be found here and here (thanks Reed Copsey):
From the first link:
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
...
The access level for class members and struct members, including nested classes and structs, is private by default.
...
interfaces default to internal access.
...
Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.
From the second link:
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
And for nested types:
Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private
From MSDN:
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.
Members of
Default member accessibility
Allowed declared accessibility of the member
enum
public
None
class
private
publicprotectedinternalprivateprotected internalprivate protected
interface
public
publicprotectedinternalprivate*protected internalprivate protected
struct
private
publicinternalprivate
* An interface member with private accessibility must have a default implementation.
Source: Accessibility Levels (C# Reference) (September 15th, 2021)
By default, the access modifier for a class is internal. That means to say, a class is accessible within the same assembly. But if we want the class to be accessed from other assemblies then it has to be made public.
By default is private. Unless they're nested, classes are internal.