Wednesday 19 April 2017

Technical Interview Question Answer

Generics:

1. Generics separate the logic from the datatype,in this way we increase reusability .
2. Parameterized type is called template in c++ and generics in c#.
3. It is mainly used for create  collection classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace test
{
    public class Generic_Example<UNKNOWNDATATYPE>
    {

        public bool Compare(<UNKNOWNDATATYPE x, UNKNOWNDATATYPE y)
        {
            if(x.Equals(y))
            {
                return y;
            }
            else
            {
                return x;
            }
        }
    }

    static void Main(string[] args)
    {
        Generic_Example<int> obj = new Generic_Example<int>();
        bool b1 = obj.Compare(1, 1);

        Generic_Example<string> objstring = new Generic_Example<string>();
        bool b2 = objstring.Compare("Sandeep", "Pranali");
    }
}




Difference Between string and String :

string  & String 
1. It is a data type in c#.
2. System.String is a type in the CLR.
When you use C# together with the CLR string  will be mapped to System.String.
Example : String s="I ma String";
                  string s="I am string";
3. string is a keyword and you can not  use it as an identifier.
4. String is not a keyword and you can use it as an identifier.
    string is for variables
5. String is for calling other String class methods .
    
          string fName = "Sandeep";
           string lName = "Jamkar";
        string fullName = String.Concat(fName, lName);
        if(String.IsNullOrEmpty(fName))
        {
            Console.WriteLine("Enter First Name");

        }

Var key word in C#. :

1. var key word is an implicit way of defining data type.
2. In simple words var key word looks like the data right hand side or accordingly 
creates data type of the left hand.

    int i=0;==>Explicit[Direct]
you are directly defining the datatype.

var i=0;==>Implicit[Indirect]
By looking right hand side the left hand data type is created.

3. var keyword define data type statically or not on runtime. 

Uses:
Long class names makes code readable 
Linq and anonymous type

No comments:

Post a Comment