0

enter image description here

I don't know how to add a class library here, how does it turn into this?

enter image description here

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
  • It's not clear what library you're trying to add or why. You may start by learning to first create a library. The following may be of interest: Tutorial: Create a .NET class library using Visual Studio
    – user246821
    Commented Feb 28 at 15:38
  • If you follow the tutorial mentioned above, you'll discover that a library is it's own project and Visual Studio has a project template for it. If you're using .NET ensure you choose the one for .NET. If you're using .NET Framework ensure you choose the one for .NET Framework.
    – user246821
    Commented Feb 28 at 15:44
  • If your library needs to be used in both .NET and .NET Framework, then read about .NET Standard.
    – user246821
    Commented Feb 28 at 15:50

3 Answers 3

0

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.

0

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.

0
  1. Start Visual Studio Code - Select File > Open Folder from the main menu. In the Open Folder dialog, create a ClassLibraryProjects folder and click Select Folder.

  2. Open the Terminal by selecting View > Terminal from the main menu.

  3. Create a solution - In the Terminal, enter the following command:

    dotnet new sln
    
  4. Create a class library project - Add a new .NET class library project named StringLibrary to the solution.

  5. In the terminal, run the following command to create the library project:

    dotnet new classlib -o StringLibrary
    

    (The -o or --output command specifies the location to place the generated output.)

  6. Add the library project to the solution. Run the following command:

    dotnet sln add StringLibrary/StringLibrary.csproj
    
  7. Check the library targets. In Explorer, open StringLibrary/StringLibrary.csproj

Please make sure you have the .NET 8 SDK and Visual Studio Code with the C# extension installed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.