none
What is the best project type to create a library in C#?

    Question

  • Hi.

    I am using Visual Studio Community 2017.

    Let's say I want to create a library and that there is no reason that would make it usable only in one context (maybe because it is completely unrelated with the interface. Imagine for instance a maths library).

    Now, what project should I create?

    A .DLL is not the best way to go, because it will only be usable on Windows Desktop.

    Other project types have similar compatibility issues.

    Of course, I could create a shared project, but if I do:

    - I will not be able to have a separate file on Windows Desktop.

    - The internal access modifier will no longer work.

    My solution is to:
    - Create a shared project and develop the library in it.

    - Create a Class Library project (or whatever I need in the context), delete Class1 and reference the shared project.

    - Let my applications reference the Class Library project instead of the shared project.

    Of course I can also create other projects, like a .NET Standard one.

    Is this solution of using bad projects a bad one? Are there better ones I didn't think of?

    Thursday, April 20, 2017 7:20 PM

All replies

  • Hi friend,

    As this issue is more related to C# developing, I will help you to move the corresponding forum for you to get a professional and quick answer. Thanks for your understanding.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 21, 2017 7:28 AM
  • Hi Aspie96,

    Thank you for posting here.

    The class library is better way than shared project. But class library is dependent on various language( c#,C++).

    For the dependent issue, i would suggest you consider the component object model(COM) that is a platform-independent, distributed, object-oriented system for creating binary software components that can interact. COM is the foundation technology for Microsoft's OLE (compound documents) and ActiveX (Internet-enabled components) technologies.

    For more information, see the COM document.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Hart Wang Friday, April 21, 2017 8:40 AM
    Friday, April 21, 2017 8:37 AM
  • Hi Aspie96,

    What you could do is change the output of your project from properties to Class Library. This will create a DLL file referencing with all the class file of your project. Hope this helps you.

    class output


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, April 21, 2017 9:37 AM
    Moderator
  • In regards to not having separate files on the desktop when considering using a class project/dll/library then you can add the dll to the GAC (see this page for an idea how to). When added to the GAC you then reference it no different than any other .NET standard dll in the project. When distributing the app adding to the GAC needs to be in the install of the app.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 21, 2017 11:01 AM
    Moderator
  • Thank you for your answer.

    The class library is better way than shared project.

    Even if I, as described, create an empty Class Library referencing the shared project? Or you ment if I just create a shared project?

    But class library is dependent on various language( c#,C++).

    Actually one can use a class library in a project developed in a different language.

    And the same goes for a shared project, if I create the wrapper class library as described.

    For the dependent issue, i would suggest you consider the component object model(COM) that is a platform-independent, distributed, object-oriented system for creating binary software components that can interact. COM is the foundation technology for Microsoft's OLE (compound documents) and ActiveX (Internet-enabled components) technologies.

    I really must check that out, so thank you for linking it.

    The thing is, though, that in Visual Studio Community there are many types of library.

    Class Library is for Windows Desktop.

    But then I have many others, such as Android Class Library, iOS class library and so on.

    If I am developing a library that is completely independent from the platform, how am I supposed to pick one of the above over the others? "Class Library" just has a shorter name, but should really be "Windows Desktop Class Library".

    Friday, April 21, 2017 11:57 AM
  • Thank you for your answer.

    What you could do is change the output of your project from properties to Class Library. This will create a DLL file referencing with all the class file of your project. Hope this helps you.

    I am not trying to convert a project from one type to another.

    What I want is to create a platform-independent C# library.

    Friday, April 21, 2017 12:00 PM
  • Cross platform options

    https://msdn.microsoft.com/en-us/library/dn720832%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Cross-Platform Development with the Portable Class Library

    https://msdn.microsoft.com/en-us/library/gg597391(v=vs.110).aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 21, 2017 12:38 PM
    Moderator
  • Hi Aspie96!

    As you said it yourself, you have many different possible approaches, some of which have been proposed in the preceeding answers. My personal favorite way to go is SharedProjects, as the backbone of the library, and both a DOTNET45 project (for Mono's sakes and for traditional .NET apps) and a PCL (Portable Class Library) project referencing the SharedProjects. This is particularly usefull if you intend to package your library, letting nuget take care of the assemblies to reference depending on the context.

    Another way to go is the DotNetCore, but depending on your needs, you may want to skip that one, for it is still in its early days and do not yet implement the whole traditional .Net Framework.

    Hope this helps!

    Chad

    Friday, April 21, 2017 4:04 PM
  • As you said it yourself, you have many different possible approaches, some of which have been proposed in the preceeding answers. My personal favorite way to go is SharedProjects, as the backbone of the library, and both a DOTNET45 project (for Mono's sakes and for traditional .NET apps) and a PCL (Portable Class Library) project referencing the SharedProjects.

    This is pretty much what I proposed, but that would mean, of course, that the other projects are emtpy and they just reference the shared project.

    What I am trying to understand is if that is a bad practice for some reason, or if it is perfectly fine to do.

    Saturday, April 22, 2017 7:55 PM
  • Well, the referencing projects won't necessarily be empty, for you will probably have to had DOTNET-specific and PCL-specific implementations.

    I would recommend, by the way, having a look at Type Forwarding, for I'm sure you'll eventually need it if you choose to proceed with this kind of projects.

    I do believe it is very fine to do such projects, the only counterpart being their tricky management. I had read somewhere, though I can't find the article no more, that it is even THE way to go to target multiple platforms and environments, especially if you are looking to distribute your libs via Nuget packages.

    I'll try to find back this article as well as specific recommandations regarding this way of proceeding and will post them back to you asap ;)

    Sunday, April 23, 2017 7:44 AM