46

I'm learning how to use dotnet-cli with VSCode. I've seen many commands on how to create solution, projects, add reference to projects... but I don't see anywhere in the documentation how to add a file. If remember, in RubyOnRails it was possible to add file from the command line.

Thanks for helping

3
  • Even I have not encountered any documentation for this. I prefer it from vs code UI. Commented Mar 3, 2018 at 18:13
  • I see. It's those boilerplate code, such as using statement, namespace, class definition, that I'm after. However, if there's none, that's ok too. I'll just write them myself. Commented Mar 3, 2018 at 18:18
  • Are you wanting simply to create a new file or are you wanting to create a file with content that follows a certain template? Commented Mar 5, 2018 at 19:24

12 Answers 12

10

Not that I know of (I was also researching that topic) but I found helpful extension for VS code called C# Extensions by jchannon, which helps you create classes and interfaces with correct namespace by right-clicking the folder in VS code explorer and selecting to create C# class or C# interface.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately it appears this extension is on longer under development.
It is, but by a different author (see here). Works well.
8

If you’re after plain old C# classes then yeah, I’m not aware of cli support. However, VS Code can create classes with the “right” namespaces by right-clicking on a folder and selecting Add new class (or somesuch).

However the dotnet aspnet-codegenerator can create boilerplate classes for various elements of an MVC application. Here is a discussion.

3 Comments

Note that you need the extension "C# Extensions" installed for the right-click options to show up.
it would be great to have a mouse-free option. Does the "C# Extensions" vscode extension have robust keyboard shortcuts?
It is no longer under development.
6

just type

new-item YourCSharpFileName.cs

i know this is not your point but; this simple answer must stay here

this powershell command doesn't create a templated csharp class but; if you like or find easier to add a new file just by typing. powershell, command prompt or any terminal... it could be very helpfull. add your file than configure the others(namespace, usings...)

additionaly for git bash use touch YourFileName.cs

i hope this help someone. if anything wrong please comment me.

2 Comments

This generates the files yes but it does not generate a namespace nor a corresponding class inside the respective files. Nice though.
thank you for your comment @Nulle. i have only pointed out easyness of the terminal creating file. snippets could be used for the class or interface or controller or other... c sharp types.
4

Just add the file in the same directory or sub-directory then build the project using Core CLI, it will be added. As per the Core CLI documentation by default all the source files are included in the build, without being added into the project files, though there are option available to override this default behaviour.

Comments

4

Disclaimer: This is for the .NET 8 SDK which includes the .NET CLI. There is no need to install a template to create a class using the .NET CLI.

To create a class using the dotnet new command in the .NET CLI type

dotnet new class -n ClassName

The -n switch is for class name. This creates a class that contains (MyProject is the name of the containing folder):

namespace MyProject;

public static class MyClass
{

}

If you created a console project using the .NET CLI, the namespace will not be needed. If you want to see more options for class creation using the .NET CLI type

dotnet new class -h

Happy .NET CLI coding!

Comments

1

Visual Studio has an UX that allows you to right-click a project or a folder underneath a project and open a context window. Selecting Add brings up a modal that allows you to select a file template. This intelligently adds the corresponding file to the location specified, adding a calculated namespace and default using statements that are typical for that file type. These item templates are backed by .vstemplate files and complemented by a file of that type. For example, for a C# class, there is a Class folder with a Class.cs file and a Class.vstemplate file. The former is a template for a C# file, the latter is an XML-based file that describes the template, per the XML namespace described here. This is a starting point.

According to Tutorial: Create a project template for dotnet new,

With .NET Core, you can create and deploy templates that generate projects, files, even resources. This tutorial is part two of a series that teaches you how to create, install, and uninstall, templates for use with the dotnet new command. [emphasis added]

This blog post, How to create your own template for dotnet new, has some examples how to add replaceable parameters and optional content, which would be good for adding namespaces, class names, class keywords, etc.

Based on this information, my next step would be to try and create some dotnet new file templates. After the basics of creating a file, I would then experiment with how to make them smarter, like these VS Templates, using replaceable parameters and optional content.

Comments

1

dotnet new provides many templates like mvc, webapp, classlib, etc. but doesn't provide templates for class, interface, struct, etc. ...

I searched and found that we can install these templates using command

dotnet new -i Yae.Templates::0.0.2

and then you can use command in your preferred directory

dotnet new class -t <nameOfClass_without_extension_.cs>

Comments

1

Install the template first

dotnet new --install Yae.Templates

Then you can use

dotnet new class -t filename -o foldername

Comments

1

I just tried dotnet new class -h to see the available options.

So, we can use something simple like this dotnet new class -n users

enter image description here

Comments

1

I came up with:

dotnet new class --name MyClass --language C#

Which produced a *.cs file containing:

namespace MyNamespace;

public class MyClass
{

}

I am using .NET SDK 8.0.201 and the latest dotnet CLI. I noticed that the namespace is always the same as the project namespace rather than the current folder namespace. You will need to set the namespace to your preference.

Comments

0

I used to use the plugin C# Extensions which is no longer under development. Today I found a replacement extension for the new class/interface functionality called "C# Stretch": https://marketplace.visualstudio.com/items?itemName=jacokok.csharp-stretch

Its a context-menu item in the file explorer allowing you to type in a class name. It produces a C# file with an empty class with the namespace declared in the new file-scoped manner. The nesting namespace format is available as an option in settings.

Comments

0

Install the template first:

dotnet new --install Yae.Templates

To create new class file:

dotnet new class -ty "YourClassName"

1 Comment

dotnet new --install Yae.Templates shows a depcrecated warning (about --install). Use dotnet new --uninstall Yae.Templates to remove this templates, and dotnet new class -n YourClassName to create a new YourClassName.cs class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.