7

I'm learning C# and I don't know exactly how to make a class visible to all projects inside the Solution.

Basically I have many projects, all Windows Forms, all of them have some similarity, so they can share a lot of classes. I think a good approach is put all the shared classes in the Solution folder and make all the project (apps) get the classes from there.

Is this a good approach?

And how to do it?

Another way to do it, I think, is to put all the shared classes in one project (app) and let the other projects refer to that project.

Is there a better approach?

I'm using Visual Studio 2019 and this is what my Solution looks like that I haven't been able to share the classes between the projects:
enter image description here

1
  • 7
    You put it in a class library, aka another project and you reference that class library in your projects Commented Feb 6, 2021 at 23:45

3 Answers 3

8

Create a Class Library project within your solution and put your shared classes in there:

  • Right click on your solution in the Solution Explorer -> Click Add -> Click New Project... Add -> New Project...

  • Choose Class Library (and name the library something in the next step) Add a new Library

Then in each of the projects that you want to access the classes from:

  • Right click on Dependencies -> Click Add Project Reference... Right click Dependencies

  • Tick the class library project you created enter image description here

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

3 Comments

There is a lot of options like "Class Library (.NET Standard)", "WPF Custom Control Library (.NET)", etc. I think the best option would be "Class Library (.NET Core)", isn't?
I can see that your other projects are .NET Core, so yes
I find this answer not useful. The new library solution creates an additional DLL. But how to include common C# class files into multiple projects, WITHOUT needing to compile and deliver an additional DLL?
1

You create a class library project and in there you would want to add folders where you can name them according to what the classes would do so it is easier to maintain. Then you would add your classes in the corresponding folder and in the project that you want to use the class you can add a reference to the project and the class would be accessible as long as it was a public class.

Comments

-1

Explanation

This is a bash script. The sooner you understand how to use dotnet CLI, the better.
CLI rarely changes. And it's like programming, not a recommendation to press a certain button.
In general, you don't know yet whether you will code in Rider or VS.
Eventually, you will be able to look at and debug the dotnet code itself, since it is open source.
Everything ends up in scripting. Otherwise, you will spend years wondering what that magic button does.
This script should be executed in bash console. It might be better to write it in powershell, but for things like this, I think bash is better.
cat with EOF is a very convenient command.
cat --help - explanation. or see https://man7.org/linux/man-pages/man1/cat.1.html
Try typing dotnet --help in any console to practice and understand.
On the left is the variable name, on the right is its value.
# starts comment.
Check it with the echo command, for example

sln=Abba
echo $sln
echo $sln.BoneyM

Scripts

    sln=mysln
    p1=$sln.app
    p2=$sln.lib1
    ver=net8.0
    dotnet new console  -n $p1 -f $ver
    dotnet new classlib -n $p2 -f $ver
    dotnet new  sln -n $sln
    dotnet sln add ./$p1/${p1}.csproj
    dotnet sln add ./$p2/${p2}.csproj
    cd $p1
    dotnet  add reference ../$p2/${p2}.csproj # add ref
    cd ..
    # create Baz.cs in library
    cat > ./$p2/Baz.cs <<EOF

    namespace $p2;
 
    public class Baz
    {
     public int Prop=1;
    }
    EOF
    # update program.cs
    cat > ./$p1/Program.cs <<EOF
    using $p2; 
    var boo= new Baz();
    Console.WriteLine(boo.Prop);
    EOF

    dotnet run  --project ./${p1}/${p1}.csproj  # test

2 Comments

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?
I see what you're trying to do, but this is less than helpful for the majority of (new) C# users, especially those using Windows.

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.