Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reformat a string of C# code to get it properly indented?

I've found that if I copy C# code from off of a website, and paste it into Notepad++, it can look ugly. Basically it's all on 1 line. Is there a way to format code, e.g. a way to take something like 1 long line of C# code, and then properly indent the code?

like image 551
Rod Avatar asked Dec 05 '22 19:12

Rod


2 Answers

If you're trying to format a C-style language you can in fact use Notepad++ - but you'll need to install TextFX. You can do that via the Plugin Manager (Plugins -> Plugin Manager) or by downloading the ZIP from here. Once you've downloaded the ZIP just drop that DLL into the Plugins folder for Notepad++ (which is going to be under the install directory) and (re)start Notepad++.

Then when you want to use it use the TextFX -> TextFX Edit -> Reindent C++ Code option from the menu. It will make something like this:

namespace Hello
{
public class MyClass
  {
 }
}

look like this:

namespace Hello
{
    public class MyClass
    {
    }
}
like image 129
Mike Perrenoud Avatar answered Apr 16 '23 16:04

Mike Perrenoud


In Visual Studio, when you paste the code, you can go to Edit --> Advanced --> Format Document

In Notepad++ you may be out of luck on the single line thing, but you can go to Language --> C --> C# (or language of code)

like image 23
Ray K Avatar answered Apr 16 '23 15:04

Ray K