Various Build Actions in Visual Studio

Hello everyone, In this post we are going to learn about various Build Actions of Visual Studio. The Build Action property indicates what Visual Studio does with a file when a build is executed. 

Actually you can change build action of each and every file in any visual studio project. Each build action has its own functionality as described below:

  • None: The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
  • Compile: The file is compiled into the build output. This setting is used for code files.
  • Content: Allows you to retrieve a file (in same dir as assembly) as a stream via Application.GetContentStream( uri ). For this method to work, it needs a AssemblyAssociatedContentFile custom attribute which VS graciously adds when you mark a file as “Content”
  • Embedded resource: embeds the file in an exclusive assembly manifest resource.
  • Resource (wpf only): embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources.
  • Page (wpf only): Used to compile a xaml file into baml. The baml is then embedded with the same technique as Resource (i.e. available as `AppName.g.resources)
  • ApplicationDefinition (wpf only): Mark the XAML/class file that defines your application. You specify the code-behind with the x:Class=”Namespace.ClassName” and set the startup form/page with StartupUri=”Window1.xaml”
  • SplashScreen (wpf only): An image that is marked as SplashScreen is shown automatically when an WPF application loads, and then fades
  • DesignData: Compiles xaml viewmodels so that usercontrols can be previewed with sample data in visual studio (uses mock types)
  • DesignDataWithDesignTimeCreatableTypes: Compiles xaml viewmodels so that usercontrols can be previewed with sample data in visual studio (uses actual types)
  • EntityDeploy: (Entity Framework): used to deploy the Entity Framework artifacts
  • CodeAnalysisDictionary: A xml file containing custom word dictionary for spelling rules

 

Common Questions / Issues associated with understanding Build Action:

  1. My class under App_Code / App_Data is not accessible in Visual Studio .NET
  2. Cannot reference App_Code in .NET

Resolution:

The above questions mostly asked with the ASP.NET development, The answer is simple change the Build Action of the files under App_Data from Content to Compile.

If you have trouble to find build action, kindly follow the steps below.
  1. Select the file under App_Code / App_Data
  2. Go to Properties window
  3. Change Build Action to Compile

Types of Constructor in .Net

Constructor is nothing but a function (with the same name as Class) and is used for initializing the members of a class whenever an class object is created. You can initialize the members with any value (as per member datatype) or with the default values through constructor.

If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor.

A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.

Some of the basic properties of Constructor are :

  1. Constructors do not return a value.
  2. Constructors can be overloaded.
  3. If a class is defined with static and Non-static constructors then the privilege will be given to the Non-static constructors.

Using the code

A. Public Constructor:

These are the most common, widely used and simple to implement constructors in Object Oriented Programming. The constructor is defined as public and get be called whenever a class is instantiated from external location of your program. Public Constructors is also known as default constructor of any class.

Also the Classes without a specified public constructor will have an implicit public constructor and maintained internally by CLR. Public constructor cannot be used in abstract class because public constructors always create instances of a type, and you cannot create instances of an abstract type.

[ads1]

using System;

class MyClass
{
 // defalut public constructor
 public MyClass()
 {
   Console.WriteLine("This is default constructor");
 }
 //public constructor with parameter
 public MyClass(int a)
 {
  Console.WriteLine("Value is "+a);
 }
}

class MyProgram
{
// start execution here from main method.
static void Main()
 {
 // initialize the class object
 MyClass myclass1 = new MyClass(); // default constructor
 MyClass myclass2 = new MyClass(5);
 Console.ReadLine();
 }
}

B. Protected Constructor:

A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created.

Means protected constructor is used with an abstract class where object initialization could not happen (because of abtract nature). The base class initialization happen only when derived class is initialized and call base class constructor.

 

public class Base
{
 protected Base() { }
 protected static void staticFoo() { }
 protected void instanceFoo() { }
}

public class Derived : Base
{
 Derived()
 : base() // initialize protected constructor through derived constructor
 { }
// start execution here from main method.
 void Main()
 {
  Base b = new Base(); // Compiler Error: Can not access protected member
  b.instanceFoo(); // Compiler Error: Can't call a protected instance method on a Base.
  Base.staticFoo(); // You call protected static method
  Derived d = new Derived();
  d.instanceFoo(); // Can call an inherited protected instance method on a Derived
  Derived.staticFoo(); // Can access static method of base class.
 }
}

C. Private Constructor :

Private constructor is a soul of Singleton pattern and used whenever we have to restrict the initializatio of class using ‘new’ keyword from outside world. The private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.

 

public class MyClass
{
 private MyClass() { } // Private constructor
 public static int iCount;
 public static int IncrementCount()
 {
  return ++iCount;
 }
}
class MyTestClass
{
 // start execution here from main method.
 static void Main()
 {
  MyClass myclass = new MyClass(); // Compiler Exception: Can't initialize class because of private constructor
  MyClass.iCount = 100; // static variable directly accessible and initialized
  MyClass.IncrementCount(); // Direct call to static method
  Console.WriteLine("New value: {0}", MyClass.iCount);
 }
}

Private constructor plays a very special role in singleton design pattern. Singleton class is same as static class and both are used relatively for similar purpose (to provide only one “instance”) but still they have major difference.

  1. Singletons class can implement interfaces or derive from useful base classes.

  2. Singleton class object can be use as a parameter which is not possible for Static class.

  3. Singletons can be handled polymorphically without forcing their users to assume that there is only one instance.

 

class Program
{
static void Main(string[] args)
{
// get the class object without initializing through 'new' keyword
MyClass instance = MyClass.Instance;
}
}
/// <summary>
/// Sealed class. Implemented through Singleton pattern.
/// </summary>
public sealed class MyClass
{
// The internal static valiable of class object
static readonly MyClass _instance = new MyClass();
/// <summary>
/// This is a private constructor, meaning no outsiders have access.
/// </summary>
private MyClass()
{
// initailize the internal variables
}
/// <summary>
/// The Instance object of an MyClass class.
/// Return the _instance object everytime whenever the MyClass class is initialized.
/// </summary>
public static MyClass Instance
{
 get { return _instance; }
}
}

D. Static Constructor :

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once.

It is called automatically before the first instance is created or any static members are referenced. Static Constructor is special in nature as a static constructor does not take access modifiers or have parameters and can’t access any non-static data member of a class.

A static constructor cannot be called directly and the user has no control on when the static constructor is executed in the program.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. The static constructors are basically useful when creating wrapper classes for unmanaged code.

 

class Program
{
 static void Main(string[] args)
 {
 MyClass.foo(); // call the static member directly
 MyClass myclass = new MyClass();
 myclass.foo(10);
 Console.ReadLine();
 }
}

public class MyClass
{
 static int a;
 int b;
 // Static constructor:
 static MyClass()
 {
  a = 10; // initialize the static variable
  //b = 20; // Compile Exception : non-static member can't be access through static constructor
  Console.WriteLine("I am in static constructor");
 }
 // non-static constructor
 public MyClass()
 {
  a = 10; // non-static constructor can initialize both static and non-static members
  b = 20;
  Console.WriteLine("I am in non-static constructor");
 }
 public static void foo()
 {
  Console.WriteLine("I am in static foo");
 }
 public void foo(int a)
 {
  Console.WriteLine("I am in non-static foo");
 }
}

Something More

Constructor Overloading : A constructor with zero arguments is known as the default constructor. A constructor can take zero or more arguments as parameters. The program have multiple constructors in a class with different sets of signatures. Such constructors are known as “overloaded constructors”. The overloaded constructors of a class must differ in their number of arguments, type of arguments, and/or order of arguments. This gives the user the ability to initialize the object in multiple ways.

The class in the program shown below contains three constructors. The first is the default constructor, followed by the two argument constructors. Note that the constructors differ in their signature.

using system;
public class MyClass
{
 public MyClass()
 {
 //Default constructor
 }
 public MyClass(int sampleValue)
 {
 // This is the constructor with one parameter.
 }
 public MyClass(int firstValue, int secondValue)
 {
 // This is the constructor with two parameters.
 }
}

Constructor Chaining : 

Constructor chaining refers to the ability of a class to call one constructor from another constructor. In order to call one constructor from another, we use base (parameters) or : this (parameters) just before the actual code for the constructor, depending on whether we want to call a constructor in the base class or in the current class.

using system;
public class MyClass
{
public MyClass(): this(10)
{
// This is the default constructor
// calling base class constructor and passing value 10 as parameter
}
public MyClass(int firstValue)
{
// This is the constructor with one parameter.
}
}

Conclusion

The best practice is to always explicitly specify the constructor, even if it is a public default constructor. Proper design of constructors goes a long way in solving the challenges faced in class designs.