Sunday, 27 January 2019

IQ

Where unmanaged code executed in c#
Unmanaged code is executed with help of wrapper classes.
Wrapper classes are of two types: CCW (COM Callable Wrapper) and RCW (Runtime Callable Wrapper).
Wrapper is used to cover difference with the help of CCW and RCW.

  1. Why to use C# using statement

C# provides a special "using" statement to call Dispose method explicitly. using statement gives you a proper way to call the Dispose method on the object. 
In using statement, we instantiate an object in the statement. At the end of using statement block, it automatically calls the Dispose method.

How to use C# using statement

Below is syntax for using statement:
1
2
3
4
5
using(SqlHelper sqlHelper = new SqlHelper())
{
       // use sqlHelper object
} //automatically calls Dispose method
sqlHelper object is not available outside the using statement.

How using internal work

using statement convert your code block to try..finally block internally like shown below:
1
2
3
4
5
6
7
8
9
10
11
12
SqlHelper sqlHelper = new SqlHelper();
try
{
    // use sqlHelper object
}
finally
{
    if (sqlHelper != null)
    {
        ((IDisposable)sqlHelper).Dispose();
    }
}


What is Ajax
How to make service call as asyncronous
Dependecy injection in c#
Dcom in IIS
What are the issues you are facing while deployment in IIS

string reverse
static void Main(string[] args)
    {
        string Str, reversestring = "";
        int Length;
        Console.Write("Enter A String : ");
        Str = Console.ReadLine();
        Length = Str.Length - 1;
        while (Length >= 0)
        {
            reversestring = reversestring + Str[Length];
            Length--;
        }
        Console.WriteLine("Reverse  String  Is  {0}", reversestring);
        Console.ReadLine();
    }

collasition?

swap even and odd characters in a string

static void Main(string[] args)
        {
            string input = "Gupta"; //Sample String
            // O/P--uGtpa
            StringBuilder output = new StringBuilder();

            char[] characters = input.ToCharArray();

            for (int i = 0; i < characters.Length; i++)
            {
                if (i % 2 == 0)
                {
                    if ((i + 1) < characters.Length)
                    {
                        output.Append(characters[i + 1]);
                    }
                    output.Append(characters[i]);
                }
            }

            Console.WriteLine(output);
            Console.ReadLine();
        }

 

 remove duplicate characters from string.


 void removeDuplicate()
    {
      string value1 = RemoveDuplicateChars("Devarajan");
    }

     static string RemoveDuplicateChars(string key)
    {

        string table = "";
        string result = "";          
        foreach (char value in key)
        {
            if (table.IndexOf(value) == -1)
            {
                table += value;
                result += value;
            }
        }
        return result;
    }
Number of occurrences in a given string
 static void Main(string[] args)
        {
            string input = "ABCA";
            while (input.Length > 0)
            {
                Console.Write(input[0] + " : ");
                int count = 0;
                for (int j = 0; j < input.Length; j++)
                {
                    if (input[0] == input[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                input = input.Replace(input[0].ToString(), string.Empty);
            }
            Console.ReadLine();
        }
        //O/P
        //A-2
        //B-1
        //C-1


Inheritance from multiple interfaces with the same method name
public interface ITest {
    void Test();
}
public interface ITest2 {
    void Test();
}
public class Dual : ITest, ITest2
{
    void ITest.Test() {
        Console.WriteLine("ITest.Test");
    }
    void ITest2.Test() {
        Console.WriteLine("ITest2.Test");
    }
}

Difference between ref and out parameters
static void Main(string[] args)
        {
            int val1=1; //must be initialized 
            int val2; //optional

            Example1(ref val1);
            Console.WriteLine(val1); // val1=1
            //Console.ReadLine();

            Example2(out val2);
            Console.WriteLine(val2); // val2=2
            Console.ReadLine();
        }

        static void Example1(ref int value) //called method
        {
            value = 1;
        }
        static void Example2(out int value) //called method
        {
            value = 2; //must be initialized
        }
        //O/P--1,2


Difference between task and thread
Why we need Task
  1. It can be used whenever you want to execute something in parallel. Asynchronous implementation is easy in a task, using’ async’ and ‘await’ keywords.
Why we need Thread
When the time comes when the application is required to perform few tasks at the same time.

How to create Task

  1. static void Main(string[] args) {  
  2.     Task < string > obTask = Task.Run(() => (  
  3.         return“ Hello”));  
  4.     Console.WriteLine(obTask.result);  

How to create Thread

  1. static void Main(string[] args) {  
  2.     Thread thread = new Thread(new ThreadStart(getMyName));  
  3.     thread.Start();  
  4. }  
  5. public void getMyName() {} 

Differences Between Task And Thread

  1. The Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.
  2. The task can return a result. There is no direct mechanism to return the result from a thread.
  3. Task supports cancellation through the use of cancellation tokens. But Thread doesn't.
  4. A task can have multiple processes happening at the same time. Threads can only have one task running at a time.
  5. We can easily implement Asynchronous using ’async’ and ‘await’ keywords.
  6. A new Thread()is not dealing with Thread pool thread, whereas Task does use thread pool thread.
  7. A Task is a higher level concept than Thread.