Answered Is there any way to delete or rename a class?

Joined
Aug 10, 2020
Messages
19
Programming Experience
1-3
Hi. I'm still new to this, and I was trying to create a class for my solution, and I didn't realize you're supposed to name the class in the first screen before you hit okay. Now I have this lame class1 class in my app instead of the correct descriptive name. I quit VS without saving, but when I reloaded it, the stupid class was still there. I guess it autosaved it even though I never want to do that. Am I just stuck with this class? If there is a way to delete it, it is not simple or easy, as far as I can tell. Thanks!
 
If you rename your class inside the file, VS2019 will detect this and offer to rename the file as well.

If you don't want the file, right click on the file in the Solution Explorer view and delete it.
 
View>Solution Explorer

The root node should be the solution. The children of the solution node would be projects. Children of the project would be files, folders, etc. Your class file should be listed there.
 
If you rename your class inside the file, VS2019 will detect this and offer to rename the file as well.
I don't think that that's right. If you rename the file then VS will prompt to rename the type but not the other way around. When I change a type name, I do get the option to change the file name via a suggestion icon in the left margin but that may be ReSharper doing that, so I'm not sure that vanilla VS will do it. When renaming a file though, it's not something you have to go looking for as VS pops up a dialogue that you can't avoid seeing.
 
Oh okay I switched to folder view and found it. Thanks!
Another option would have been to click the Show All Files button, which will display all files and folders under the solution, even if they are not part of a project.
 
I don't think that that's right. If you rename the file then VS will prompt to rename the type but not the other way around. When I change a type name, I do get the option to change the file name via a suggestion icon in the left margin but that may
I've got vanilla VS2019. With the following steps, the file name changes:
0) Create a new class by right clicking on the Project node, and select Add>Class...
Accept the default poorly named "Class1".
Notice the Solution explorer shows file named "class1.cs".
1) Inside class1.cs, select Class1.
2) Press F2 to invoke the renaming wizard, or right click and select "Rename..."
3) Give the class a new name like "Frodo", and press Enter

Results:
The filename "Class1.cs" is rename to "Frodo.cs".
 
I've got vanilla VS2019. With the following steps, the file name changes:
0) Create a new class by right clicking on the Project node, and select Add>Class...
Accept the default poorly named "Class1".
Notice the Solution explorer shows file named "class1.cs".
1) Inside class1.cs, select Class1.
2) Press F2 to invoke the renaming wizard, or right click and select "Rename..."
3) Give the class a new name like "Frodo", and press Enter

Results:
The filename "Class1.cs" is rename to "Frodo.cs".
Ah, OK. I was think about simply retyping the name in the code rather than using a dedicated tool. I've never used that option before as I don't think that I've ever needed to but it's always good to know what options are available.
 
I'd say that's likely going to be a feature in one of the next releases in VS, because Microsoft and the compiler preference that a class definition be named the same as the filename. They also preference to have one class per file and that doesn't include nested classes.
 
Back
Top Bottom