Shared project in Visual Studio 2015Shared 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!