Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, October 24, 2012

Initialize Struct members with default values in C#

Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Explore the Top Microsoft Blazor Technical/ Interview Questions here: https://XploreBlazor.blogspot.com/

C# structs cannot have a default constructor. Writing a default parameter-less constructor would not compile, since there is one already predefined by the compiler.

For example, the following code would result in the compilation error, 'Structs cannot contain explicit parameterless constructors' :

public struct Employee
{
      public int EmpNo;
      public string LastName;
      public string FirstName;
      public DateTime HiredDate;
      public bool IsActive;

      public Employee()      {
                  EmpNo = 0;
        LastName= null;  
        FirstName=null;  
        HiredDate = new DateTime(1,1,1);        
        IsActive = false;      }
}

  The predefined constructor initializes all the members of the struct with its default values according to their types. For example, it initializes numeric types (int, long, float etc) to 0, bool types to false, reference types to null and datetime types to 1/1/0001 12:00:00 AM.

The struct variables are usually declared and used without the new operator since struct is a value type. For example:

public struct Employee
{
      public int EmpNo;
      public string LastName;
      public string FirstName;
      public DateTime HiredDate;
      public bool IsActive;}

Employee emp;
emp.EmpNo = 0;


But to initialize the members with the default values, call the predefined default constructor using the new operator as follows:

emp = new Employee();


The above code initailizes EmpNo to 0, LastName and FirstName to null, HiredDate to 1/1/0001 12:00:00 AM and IsActive to false

An another way to initializes the struct members to their default values, is to use default keyword as follows:

emp = default(Employee);



Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Wednesday, October 17, 2012

Clone Generic List in C# using an Extension method

Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Explore the Top Microsoft Blazor Technical/ Interview Questions here: https://XploreBlazor.blogspot.com/

See below the plain readable code for cloning a generic collection in C#:

namespace Org.Project.Common
{
  using System;
  using System.Collections.Generic;
   public static class CloneableExtension
   {
     public static IList<T> Clone<T>(this IList<T> list) where T : ICloneable
     {
        IList<T> clonedList;
        clonedList = new List<T>();
        foreach (T cloneable in list)
        {
          object clonedObj = cloneable.Clone();
          clonedList.Add((T)clonedObj);
        }
        return clonedList;

     }
  }
  
}

The method extends all collections that implements IList but returns instance of List always.
The objects of type T that the list consists of should implement ICloneable interface.

Here is another stylish implementation using LINQ:

namespace Org.Project.Common{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  public static class CloneableExtension
  {
   public static IList<T> Clone<T>(this IList<T> list) where T : ICloneable
   {     return list.Select(item => (T)item.Clone()).ToList();    }
  }
}


Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com