I don’t know how to add a class library here, how does it turn into this?
I am really lost right now since our prof did not teach much about this, I just really want to know but I am still very confused about this
I don’t know how to add a class library here, how does it turn into this?
I am really lost right now since our prof did not teach much about this, I just really want to know but I am still very confused about this
3
Answers
The best possible process to add a class library would be to follow the MicroSoft tutorial from the Microsoft Learn site.
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio-code?pivots=dotnet-8-0
But in short, you will need to create a new project just for the class library that you include your class into. Make sure the program you are going to use this class library with has a reference to your new class library project.
If you want to include a class directly in your program project, that is possible, but it will not be a separate library that you can reuse in other projects dynamically. You will have to add that code directly into other projects. Libraries are good for developing multiple applications that share a common set of class utilities that you want to ensure the code base doesn’t become redundant. If you need classes to reuse from within your main program, you may not need a library on its own.
dotnet new sln
dotnet new classlib -o StringLibrary
(The -o or –output command specifies the location to place the generated output.)
dotnet sln add StringLibrary/StringLibrary.csproj
Please make sure you have the .NET 8 SDK and Visual Studio Code with the C# extension installed.
What you are doing is adding a class called classlibrary.cs
If the second image is all you want then all you have to do is right click on the project and select -> Add -> Class.
Than name it whatever you want e.g. "ClassLibrary.cs".
Note: Class libraries are a collection of classes and namespaces in C# without any entry point method like Main, meaning it is a seperate project that you "Reference" in your main project.