Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Modifiers with Interfaces [closed]

Tags:

c#

interface

I am new to .net C#. I am learning about interfaces.

  1. Can we use access modifiers with interfaces, if so, which are valid?
  2. By default, are interfaces internal?
  3. Can we use access modifiers on interface members, if so, which are valid?
like image 326
Saurabh Mahajan Avatar asked Apr 19 '13 09:04

Saurabh Mahajan


People also ask

Can access modifiers be used for an interface?

Note: Nested interfaces and classes can have all access modifiers. Note: We cannot declare class/interface with private or protected access modifiers. For example, the following program fails in the compilation.

Which access modifier can be used with interface members?

Interface member declarations may include any access modifier. This is most useful for static methods to provide common implementations needed by all implementors of a class. Enumeration members are always public , and no access modifiers can be applied. Delegates behave like classes and structs.

When an interface method has no access modifier it is?

If no access modifier is given, the method is implicitly public . It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

What are the 3 types of access modifiers?

Access Modifiers in Java are used to define the accessibility of methods and data members. There are four main types of Access Modifiers – Default, Private, Protected, and Public.


1 Answers

Since C# 8.0 you can have access modifier inside the interface. See this post C# 8 Interfaces: Public, Private, and Protected Members


Before C# 8.0

You should see:

Access Modifier - MSDN

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

(For your questions)

Can we use Access Modifier with Interface

Yes, they can be declared as public or internal

By default Are inteface internal

Yes.

what about Access Modfiers with Inteface members.

They are public. No access modifiers can be applied to interface members.

like image 132
Habib Avatar answered Oct 21 '22 09:10

Habib