5

Is there any way for Visual Studio to create my interface 'public'? For example, right click on folder -> create new item -> code -> interface.

Whenever the file is created there are no access modifiers.

interface IMyInterface 
{

}

Is there any way to default it to make it public?

public interface IMyInterface
{

}

I'm always forgetting to manually change them to public (when they need to be).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Anonymous
  • 1,978
  • 2
  • 28
  • 37
  • I don't think so, because one of the main rules of programming is encapsulation. Now if everything would be public, this rule wouldn't make any sense any more. In my opinion you should better rewrite your code. – jAC Jan 05 '13 at 12:18
  • 1
    possible duplicate of [How do you default a new class to public when creating it in Visual Studio?](http://stackoverflow.com/questions/700086/how-do-you-default-a-new-class-to-public-when-creating-it-in-visual-studio). Note that this link contains an answer detailing how to modify that behavior. – slugster Jan 05 '13 at 12:22
  • @JanesAbouChleih Take notice I said, "when they need to be" I don't set everything to public. That would be silly :) – Anonymous Jan 05 '13 at 12:24
  • Oh yeah, you're right. Must've overlooked that. – jAC Jan 05 '13 at 12:26

2 Answers2

5

Open your project with the public IMyInterface interface shown above.

Go to File > Export Template.

Follow the steps (make sure you choose item template rather than Project template) and save it under a name of your choice.

When you restart visual studio and have a project open it will be available from the add item dialog. You will be able to set the name of the interface from the dialog just like you can with the other items.

Note that this does not overwrite the default interface template installed with Visual Studio so you can still just as easily make a private interface when required.

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
  • Just a detail: A type (like an interface) that is not nested inside some `class` or `struct`, cannot be "private" (as you call it). Because "private" means "only accessible to the other members of the same class/struct", and this interface is not member of any class/struct. Actually for non-nested types, the only possibilities are `public` and `internal` (the latter being the default access level). Still a good answer on how to modify Visual Studio templates. – Jeppe Stig Nielsen Jan 05 '13 at 14:43
  • Yes, you are absolutely correct. Thank you for the clarification. – Benjamin Gale Jan 05 '13 at 14:44
1

You can also create a code snippet for public interfaces like this :

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>public interface</Title>
    </Header>
    <Snippet>
        <Code Language="csharp">
            <![CDATA[
    public interface IMyInterface
        {
            }
    ]]>
        </Code>
    </Snippet>
</CodeSnippet>

and save it as publicinterfacecsharp.snippet. Then go to Tools -> Code Snippets Manager...
Select language as C# and My Code Snippets folder, then click Import.. and point to location where you saved the snippet. Now in any new project, you can right click in the editor window and select Insert Code Snippet -> My Code Snippets -> publicinterfacecsharp.

prthrokz
  • 1,120
  • 8
  • 16