Renaming Class Types

AmeteurBoi

New member
Joined
Jan 22, 2020
Messages
1
Programming Experience
Beginner
Now, I made a class and i want to rename the type that I made.

//example
Book book1 = new Book();

Now, is there a way to rename "book1" after this line was put into action? I looked at the c# programming tutorial of Microsoft but couldn't find anything that explained this.

//I'm new when it comes to c# and programming in general, so I might look like an idiot here.
 
Firstly, "class type" doesn't really make sense. You just say "class" or "type". "Type" is more general and "class" is more specific. Structures, enumerations and delegates are also types.

Secondly, you're not asking how to rename a class or type. You're asking how to rename a variable. In that code, Book is the type and book1 is a variable of that type. The code declares a variable of type Book, creates an object of that type and assigns the object to the variable.

As for the question, this is certainly something that can be done fairly easily. It's called "refactoring" and some refactoring functionality is built into VS. Personally, I use a VS extension called ReSharper, which changes some of that functionality and adds more. I often don't know whether some of the functionality that I use is provided by ReSharper or VS. I'll describe what I do and you can see whether you have the same options.

Firstly, double-click the variable name to select it all. Next, overwrite the current name with the desired new name. Before typing anything anywhere else, hover the mouse over the variable name and you should see a little light-bulb icon pop up with a down-arrow. Click that and you should get a drop-down menu with an option to rename the variable. That will find all locations where the variable is used and change the name from the old to the new. That will work throughout the entire project, so it doesn't matter where the thing you're renaming is being used. You can use this same method to change the name of types, members, variables or whatever.

If you click the name after overwriting, you may also see an icon appear in the left margin of the code window that you can click on to get the rename option too. I know that ReSharper is at least partially responsible for that though, so you may not see it or the same options in it if using vanilla VS.
 
Firstly, double-click the variable name to select it all. Next, overwrite the current name with the desired new name. Before typing anything anywhere else, hover the mouse over the variable name and you should see a little light-bulb icon pop up with a down-arrow.
That is a visual studio feature. (y)
 
In plain VS (Community) right click the variable name and select Rename... write the new name and press Enter.
Same can be done with type/class name if it is defined in that project.
 
Now, I made a class and i want to rename the type that I made.
Based on how this is worded, I get the feeling that our OP is asking if this is possible :
C#:
        private static void run()
        {
            FooClass fClass = new FooClass();
            fClass second_fClass = new fClass();
        }
        internal class FooClass
        {

        }
For the same reasons pointed out above. If you paste that code into your code file, you will notice the error on fClass second...etc.... something to the effect of : fClass is a variable but used like a type. I believe our OP was asking if its possible to rename the instance of something just declared. But I think they may be trying to do it after compile-time, and not do it in visual studio.
 
Back
Top Bottom