Visual Studio 2022 Class Template Problem

Henry 41 Reputation points
2022-03-01T05:13:28.93+00:00

Hi,

I am using Visual Studio 2022. After an update, every time I add a new C# class, it starts like this:

internalclass MyClass {
}

It is annoying to see the keyword internal and class got jumbled up together like that. Is there any way I can fix this?

Thanks.

Henry

Developer technologies Visual Studio Other
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anthony 21 Reputation points
    2022-03-15T15:16:00.81+00:00

    I've just encountered the same issue. As you said, the issue comes from style preferences of using file-scoped namespaces.

    Assuming you'd always like to use file-scoped namespaces, changing the Class.cs template to the following resolved the issue for me:

    using System;
    using System.Collections.Generic;
    $if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
    $endif$using System.Text;
    $if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
    $endif$
    namespace $rootnamespace$;
    class $safeitemrootname$
    {
    }
    
    4 people found this answer helpful.
    0 comments No comments

  2. Anna Xiu-MSFT 31,056 Reputation points Microsoft External Staff
    2022-03-01T11:31:55.337+00:00

    Hi @Henry ,

    Welcome to Microsoft Q&A!

    Could you tell me the specific version of VS2022 you are using? Community, Professional or Enterprise?

    I tested on Visual Studio Community 2022 version 17.1.0 and added new C# Class item. It shows normally:

    internal class Class1  
            {  
            }  
    

    Please go to: C:\Program Files\Microsoft Visual Studio\2022\version\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.
    And check if the Class.cs template is changed or not.

    Sincerely,
    Anna
    *
    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.