User Id :    Password :      New Member   Forgot Password  
 
Shared project in Visual Studio 2015
Description Shared project is new feature that was added in Visual Studio 2013 update 2 but in visual studio 2015 it is coming by default. In this post we are going to learn about Shared Project and how it is different from class library.   No. of Views     2596
  Rating     5
Author Jalpesh Vadgama   Posted On     27 Feb 2015
Tags ASP.NET,Applications,Visual Studio,Visual Studio : How To,General    

Sample Code   Download Code

Shared project is a new feature that is introduced with Visual Studio 2013 update 2 and it’s comes by default with Visual Studio 2015. So with shared project you can share your common code with any number of applications. You can use that in another project via adding project reference. So let’s see how we can use Shared project in multiple application.

To understand how Shared project works, I have create a blank solution like following.

 

 

Then we are going to add a Shared Project in Customer Model like following.

 

 

It will create a Shared Project in solution explorer like below.

 

 

I have added a Customer Class in solution.

 

using System;
namespace CustomerModel
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
         
    }
}

 

Now let's add a console app to this solution to use this Shared Project in Console Application.

 

 

And I have added reference of CustomerModel Shared project to console application.

 

 

Now, I have added following source code for console application.

 

using CustomerModel;
using System;
 
namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer
            {
                CustomerId = 1,
                FirstName = "Jalpesh",
                LastName = "Vadgama"
            };
 
            Console.WriteLine("Firstname : {0}",customer.FirstName);
            Console.WriteLine("Lastname : {0}", customer.LastName);
 
        }
    }
}

 

Following is output as expected.

 

 

Now let’s add a windows forms application and we are going to add reference of Customer Model in that application also.

 

 

Also added Customer Model Shared Project reference.

 

 

Now I have added a button in windows forms application to show customer details.

 

 

And I have written a following code for same.

 

using System;
using System.Windows.Forms;
using CustomerModel;
 
namespace TestWindowsApplication
{
    public partial class TestWindowsForm : Form
    {
        public TestWindowsForm()
        {
            InitializeComponent();
        }
 
        private void btnShowCustomer_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer
            {
                CustomerId = 1,
                FirstName = "Jalpesh",
                LastName = "Vadgama"
            };
            MessageBox.Show(string.Format("FirstName:{0} LastName:{1}",
                         customer.FirstName, customer.LastName));
        }
    }
}

 

Now you run application and click on button. It will show output like below.

 

 

So you can see you can use shared project in multiple application so you don’t have to write code multiple times.

 

What is Difference between Class Library Project and Shared project:

 

Now lot’s people will argue that Shared Project provides same functionality as class library what are the differences? But Here are few differences.

 

  • In class library, when code is compiled assembly(dlls) are generated for each library. But with Shared Project it will not contains any header or information so when you have shared project reference it will be compiled as part of parent application. So when you use application at that time will compiled as part of console application. There will not be separate dlls created for this.

  • In class library you are only allowed to write C# code while shared project can have any thing like C# code files, XAML files or JavaScript files etc.

 

 

Hope that will clear your doubt about Shared projects. That’s it.Stay tuned for more!

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Najmul Hoda on 09 Aug 2015 at 11:07 AM
 

 This is nice article

 
 
   
Write your comment here.
Comment
Verification Code