Game library vs Shared Library

Hi, I’m a recent Monogame user. I’ve been trying to separate my game in two VS projects, one which is the standard Monogame runner, and a class library to store the game’s content. But I don’t know which one to use as there are 3 types of available library; the default C# one, the game library and the shared library. As far as I’m concerned they all do the same thing? Which one should I use to just store my assets and my model classes?

1 Like

Hey @Clem (follow this Tenno),
Welcome to the community.

The differences between a C# Library vs MonoGame Game Library vs MonoGame Shared Library are subtle, but ultimately in the end they will provide you with the same solution of sharing code from one code base among other projects.

C# Library and MonoGame Game Library

Both of these are almost the same thing, so I’ll start with the similarities first. When they are built, all of the code from these project types will be compiled into a .dll file. This .dll file is the referenced by your other projects (aka main game code), but the code from the library lives inside the .dll file that was compiled.

The difference between a C# library and a MonoGame Game library is just how they are initially setup. A C# library is more general, it’s just gives you the base .csproj file to create the library. A MonoGame Game Library is the same thing, but it come with a preconfigured .csproj that has the MonoGame NuGet references already added for you.

In the end, both of these project types will still compile their code to a .dll where that code lives and is then referenced by your other projects.

MonoGame Shared Library

A MonoGame Shared Library is actually just a C# Shared LIbrary (.shproj) that’s preconfigured with the NuGet references needed. Similar to the above in the C# Library vs MonoGame Game Library.

However there is a notable difference in how a Shared library works vs a standard Library. Recall from above that I said in a standard library the code is compiled into a .dll and lives inside that .dll? In a Shared Library, what happens instead is the code inside the Shared Library is compiled along with the projects that reference it, sort of like saying “the shared library code is copied to the projects referencing it and compiled and lives with it”

Conclusion

Ultimately, which one you use, or which is best to use, is really up to you and how you want to structure your solution. A C# Library (or MonoGame Game Library) will make simplify the process of making that code reusable with other future projects you make, and you can even create a NuGet package from them to put on NuGet.org to simplify including them in future projects. After all, they are just a .dll that other projects reference.

Alternatively, with a Shared project, the code doesn’t compile to its own .dll and instead gets copied to the projects that reference it and compiled along with them. This means for future projects, you’ll have to add the Shared Library project to the solution for each different project. You also can’t create NuGet packages from them if that is a goal in the future.

Hopefully this helps explain the differences. If what I’ve described is not clear, let me know and I’ll try to break it down further.

1 Like

Hi, thank you for your answer, it was very clear. It seems there aren’t that many practical differences between the three (if one doesn’t want to port the project as a nuget package that is).

I think going for a MG Game library seems the most practical for my project, so I’ll go for it. The Monogame ecosystem seems straightforward enough for me to get the ball rolling soon enough. I’ve not played with it for long but I’m pretty pleased by what I’ve seen so far.

2 Likes

Not sure if you’re in the discord but if you wanna join you can always ask questions there too while learning.

1 Like