Sunday, November 2, 2014

C# basic OOP interview questions and answer

Q1:What is polymorphisms?
Ans: Polymorphism is one of the very basic question for the interviewers. To answer what is Polymorphism, The most common answer we get is one interface multiple method or multiple form. We declare several method with same name but with different parameter is the easiest example of polymorphisms also known as function overloading.
Following is the example of function overloading:

using System;

class Calculator
{
    static void Add(int x,int y)
    {
        Console.WriteLine("result is:"+(x+y));
    }

    static void Add(int x,int y,int z)
    {
        Console.WriteLine("result is:"+(x+y+z));
    }
    
    static void Add()
    {
        Console.WriteLine("Invalid number");
    }
}

Following is the example of overriding also known as virtual method:

using System;

class Vehicle
{
    protected decimal avgSpeed;
   // Declared virtual so it can be overridden. 
   public virtual decimal Speed()
   {
      return avgSpeed;
   }
}

class Car : Vehicle
{
    protected decimal avgSpeed;
    protected decimal horsePower;
   // Declared virtual so it can be overridden. 
   public override decimal Speed()
   {
      return avgSpeed*horsePower;
   }
}

The override function should be define virtual. Polymorphisms could be understand easily if we understand function overloading and overriding. Operator overloading could be another example of polymorphisms.

Q2:What is abstract method?
Ans:Abstract method has no implementation. The class that inherit abstract class have to implement all the abstract method in the derived class.
Following is the example of the abstract method:
abstract class Vehicle
{
   abstract public void wheels();
   abstract public int brakes(int brakes);
   abstract public int speed();
}

class Car : Vehicle
{
   override public void wheels()
   {  
      // Initialization code goes here.    
   }

   override public int brakes(int brakes)
   {
     // Initialization code goes here.    
   }
  
   override public  public int speed()
   {
     // Initialization code goes here.    
   }
}




Q3:What is static properties of a class?
Ans: In C# a static class is same as a non static class but it is not possible to create instance of the static class. We access static class by the name of static class dot static class function or properties name.
Example: Helper.Save(); Static class does not have instance variable. Normally static class handle input variable and performs some operations. Static class remain life time of the application.
Static member can be declare in non static class. Static member is always callable by class name. Important properties to remember that static member cannot access the non static properties of the class.
Q4: What is difference between encapsulation and abstraction?
Ans: Abstraction means showing that is only important to developer. The concepts of abstraction suggest to show only minimal things to the end developer when developing software. Encapsulation described as hiding complexity of code to the developer. Insertion could be implement in different way by using array or list but that should not a problem for the end user. End user just call the method and get the expected output and don't need think about the implementation as it is hide by the concepts encapsulation.

Saturday, July 12, 2014

ASP.NET MVC 5 sample project

I will show how to create a sample asp.net mvc 5 project. First I will use visual studio 2013 web express for creating asp.net mvc 5 project. I create a new project by File> New Project > ASP.NET Web Application > MVC project. Now according to our requirements I create model schema customer.
 public class Customer 
    {
        [Key]
        public int CustomerId { get; set; }

        [Required]
        [StringLength(256, ErrorMessage = "Company cannot be longer than 256 characters.")]
        [Display(Name = "Company")]
        public string Company { get; set; }
                             
        [Required]
        [StringLength(256, ErrorMessage = "First Name cannot be longer than 256 characters.")]
        [Display(Name = "First Name")]
        public string Name { get; set; }

        [Required]
        [StringLength(256, ErrorMessage = "Last Name cannot be longer than 256 characters.")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        
        [Required]
        [StringLength(64, ErrorMessage = "Street cannot be longer than 64 characters.")]
        [Display(Name = "Street")]
        public string Street { get; set; }

        [Required]
        [StringLength(64, ErrorMessage = "Ciy cannot be longer than 64 characters.")]
        [Display(Name = "City")]
        public string City { get; set; }

        [Required]
        [StringLength(64, ErrorMessage = "Province Name cannot be longer than 64 characters.")]
        [Display(Name = "Province")]
        public string Province { get; set; }

        [Required]
        [StringLength(10, ErrorMessage = "Post Code cannot be longer than 10 characters.")]
        [Display(Name = "Post Code")]
        [DataType(DataType.PostalCode)]
        public string PostCode { get; set; }

        [Required]
        [Display(Name = "Telephone Number")]
        [StringLength(16, ErrorMessage = "Telephone cannot be longer than 16 characters.")]
        [DataType(DataType.PhoneNumber)]
        public string Telephone { get; set; }

        [Display(Name = "Fax Number")]
        public string Fax { get; set; }

        [Required(ErrorMessage = "Email Address is required.")]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Display(Name = "Registration")]
        public string Registration { get; set; }

        [Required]
        [Display(Name = "Creation Date")]
        [DataType(DataType.Date)]
        public DateTime CreationDate { get; set; }
                           
       }
I used data annotation for validating data for our schema that is consist on System.ComponentModel.DataAnnotations.Schema . The [Key] annotation work as a primary key in the schema. Then I created a contract schema.
public class Contract
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public virtual int CustomerId { get; set; }

        [ForeignKey("CustomerId")]
        public virtual Customer Customer { get; set; }

        [Required]
        [Display(Name = "Start Date")]
        [DataType(DataType.Date)]
        public DateTime StartDate { get; set; }

        [Required]
        [Display(Name = "End Date")]
        [GenericCompare(CompareToPropertyName = "StartDate", OperatorName = GenericCompareOperator.GreaterThanOrEqual)]
        [DataType(DataType.Date)]
        public DateTime EndDate { get; set; }

        [Required]
        [Display(Name = "License")]
        public Guid License { get; set; }

       
    }
The two table has One to zero -one relationship between them. So I created a customer instances in contract class with CustomerId as Foreign key. After creating model we will create a context class. The context class coordinates Entity Framework functionality for a given data model is the database context class. I added model class in the context class.
public class ByWayContext: DbContext
{
        static ByWayContext() 
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ByWayContext>());
        }

        public DbSet<Customer> Customers { get; set; }

        public DbSet<Contract> Contracts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            Database.SetInitializer<ByWayContext>(null);
        }
}
After creating model we will create controller(MVC5 Controller with View, Using entity Framework) based on our model.
Now automatically create,edit, list and delete option created for customer model class.

Search This Blog

Visitor Counter