Switch enum auto-fill

C#Visual Studio-2013

C# Problem Overview


I was typing a switch with an enum in VS 2013 and all case statements filled out automatically after I finished the switch. Now I can't repeat it. I was not hallucinating, the switch filled out with all enum options, one per case, automatically. Looked through MS docs for VS 2013 and didn't find it.

I use quite a few enums and this feature will save me a ton of time if I can find what it is I did to trigger it. Can anyone help?

C# Solutions


Solution 1 - C#

Notice: This answer applies to performing the switch/enum autogeneration while also using Resharper.

Using Visual Studio 2013 and Resharper 8.2, the previously mentioned methods do not work. Here's how to actually get this generation to work when using Resharper. Hopefully it will save someone the fifteen minutes I just spent figuring this out.

Performing "sw(tab)(tab)" will only generate the following:

switch (nameOfEnumVariable)
{

}

Resharper can generate the labels using Alt + Enter (if your cursor stands in the switch statement) and selecting Generate switch labels as in the following screenshot:


Using the Resharper menu to fill in a switch statement


The result looks like this:


enter image description here


Solution 2 - C#

Use the code snipped sw(tab)(tab)

Hope this helps,

Solution 3 - C#

The selected answer is mostly correct, you don't need Resharper as other's have suggested (at least not with Visual Studio Professional 2012+).

  1. type "sw" then "[tab][tab]" (as Marvin Smit said)

Which (as jmblack said) will generate something like:

  switch (switch_on)
  {
            default:
  }

but then

  1. you need to select which thing to enumerate on (switch_on will be highlighted still at this point). So type in the Enum (or your variable of the enum type) while switch_on is highlited and hit [Enter][Enter].

(I just confirmed this worked on my machine running VS2012, and i'm fairly certain this is the same thing i have done on my other machine running VS2013, and i haven't tested other versions of VS (ultimate/express/etc.))

Solution 4 - C#

Visual studio 2017, 2019 - without Resharper:

  1. write "switch"

  2. press two times TAB, then you will see:

    switch (switch_on) { default: }

(the switch_on is highlited)
3) retype switch_on to your enum variable or type

  1. press ENTER or click somewhere else (pressing TAB does not work), now you should see all the enum items filled:

    switch (YOUR_ENUM_VARIABLE_OR_TYPE) { case YOUR_ENUM_TYPE.Item1: break; case YOUR_ENUM_TYPE.Item2: break; case YOUR_ENUM_TYPE.Item3: break; default: break; }

Solution 5 - C#

I think what you need is this:

sw(tab)(tab)enumVariableName(tab)(downArrow)

I tested it and works ( in VS2013 at least).

Solution 6 - C#

By default, Visual Studion's snippet works correct. You should type "sw" and then press double "Tab".

If you use Resharper, the snippet doesn't work, because Resharper's snippet has more priority, by default. So, you should turn off resharper's snippet.

Go to "Resharper" -> "Template Explorer"-> "C#" then uncheck "switch". Try "sw" + double "Tab"

Solution 7 - C#

I've written a free and open source extension, based on Roslyn, for Visual Studio 2015 and 2017, that not only allows to fill the switch case for an enum, but is also capable of adding new cases if enum values (fields) have been added to the enum type definition, or sort the list of cases by value or name.

It's available here: Enum Case Generator

This is how to use it:

enter image description here

Solution 8 - C#

Hi I just ran into the same problem, I just found out that when you do:

switch(nameofvariable){
  default:
  break;
}

when you are filling the variable and you double click on the variable of your choice (the enum) it will give you all the cases

Solution 9 - C#

VS 2019 + resharper create an empty switch with your variable. Click on first curly bracket "{" Then press alt + enter You will see Generate switch labels. Screenshot below:

Generate switch labels

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMetaphorView Question on Stackoverflow
Solution 1 - C#jmsbView Answer on Stackoverflow
Solution 2 - C#Marvin SmitView Answer on Stackoverflow
Solution 3 - C#00jtView Answer on Stackoverflow
Solution 4 - C#chviLadislavView Answer on Stackoverflow
Solution 5 - C#marcelomView Answer on Stackoverflow
Solution 6 - C#GSerjoView Answer on Stackoverflow
Solution 7 - C#Simon MourierView Answer on Stackoverflow
Solution 8 - C#Cyril DelanderView Answer on Stackoverflow
Solution 9 - C#Ali KaracaView Answer on Stackoverflow