Welcome to my blog.

Post on this blog are my experience which I like to share. This blog is completely about sharing my experience and solving others query. So my humble request would be share you queries to me, who knows... maybe I can come up with a solution...
It is good know :-)

Use my contact details below to get directly in touch with me.
Gmail: nadarmuthukumar1987@gmail.com
Yahoo: nadarmuthukumar@yahoo.co.in

Apart from above people can share their queries on programming related stuff also. As me myself a programmer ;-)
Showing posts with label C# Interview. Show all posts
Showing posts with label C# Interview. Show all posts

Convert List to DataTable in C#

public DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
Reference: Muthukumar (http://nadarmuthukumar.blogspot.in) 
Hope you liked this post, also let me know your thoughts on the post through your valuable comment. 
Thank you.

Find median of an array in c#

public static int GetMedian(int[] Value)
    {
        decimal Median = 0;
        int size = Value.Length;
        int mid = size / 2;
        Median = (size % 2 != 0) ? (decimal)Value[mid] : ((decimal)Value[mid] + (decimal)Value[mid + 1]) / 2;
        return Convert.ToInt32(Math.Round(Median));
    }

Reference: Muthukumar (http://nadarmuthukumar.blogspot.in) 
Hope you liked this and let me know your thoughts on post through your valuable comment.

Thank you

Convert dd mm yyyy string to datetime

Create a function as shown below.
Your can use any one of the method give below inside the function, rest you can comment it out.
private DateTime FormatDate(string _Date)
{
        DateTime Dt;
            
        //Method 1
        System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
        dateInfo.ShortDatePattern = "dd/MM/yyyy";
        Dt = Convert.ToDateTime(_Date, dateInfo);
        //Method 2
        IFormatProvider mFomatter = new System.Globalization.CultureInfo("en-US");
        Dt = DateTime.ParseExact(_Date, "dd/MM/yyyy", mFomatter);
        //Method 3
        Dt = DateTime.ParseExact(_Date, "dd/MM/yyyy", null);
            
        return Dt;
}
To call the function use below code
string UrDate = "27/08/2008";
DateTime _obj = FormatDate(UrDate);

Reference: Muthukumar (http://nadarmuthukumar.blogspot.in)

What is an application domain (appdomain)

What is an application domain (appdomain)

Before .NET framework 2.0 technology, the only way used to isolate applications running on the same machine is by the means of process boundaries. Each application run within a process, and each process has its own boundaries and memory addresses relative to it and this is how isolation from other processes was performed.

.NET framework 2.0 introduces a new boundary called the Application Domains.

Application domain is nothing but a boundary within which an application runs.
process can contain multiple application domains. Application domains provide an isolated environment to applications.


An application running inside one application domain cannot directly access the code running inside another application domain. To access the code running in another application domain, an application needs to use a proxy.




Application domain is used to isolate executed software applications from one another so that they do not affect each other. This is achieved by making any unique virtual address space run exactly one application and scopes the resources for the process or application domain using that address space.
The .NET runtime enforces AppDomain isolation by keeping control over the use of memory - all memory in the AppDomain is managed by the .NET runtime, so the runtime can ensure that AppDomains do not access each other's memory.


Application Domain is used to create a mirror image of an application. reason being if you do not want to stop OR
down the site , .Net framefork give u feature called App Domain, through which u can make the changes and uploaded
to the site without getting it down or Stop.


The main advantage is the ability to run several applications domains in a single process or application. All of this is performed while maintaning the same level and quality of isolation that would exist in separate processes, without the need of making cross-process calls or switching between processes.

Advantage
isolation, code running in one application domain can not access code or resources running in another application domain.
security, You can control the permissions granted to a given piece of code by controlling the application domain inside which the code is running.
robustness, error in code running in one application domain can not affect other applications although they all are running inside the same process. Individual application domain can be stopped without stopping the entire process, you can simply unload the code running in a single application domain.
They are less expensive than full processes
They are multithreaded
You can stop one without killing everything in the process
Each app domain runs on its own security level



Reference: Muthukumar (http://nadarmuthukumar.blogspot.in/)
netsqlinterviewquestions, dotnetfunda, crazyengico, allinterview

Difference between class and structure

  1. The structures are value types and the classes are reference types.
    Example:
    namespace DifferenceBetweenClassesAndStructures
    {
    
        struct StrucutreExample
        {
            public int x;        
        }
    
        class ClassExample
        {
            public int x;        
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                StrucutreExample st1 = new StrucutreExample(); // Not necessary, could have done StructureExample1 st1;
                StrucutreExample st2;
    
                ClassExample cl1 = new ClassExample();
                ClassExample cl2;                        
    
                cl1.x = 100;
                st1.x = 100;
    
                cl2 = cl1;
                st2 = st1;
    
                cl2.x = 50;
                st2.x = 50;
    
                Console.WriteLine("st1 - {0}, st2 - {1}", st1.x, st2.x);            
                Console.WriteLine("cl1 - {0}, cl2 - {1}", cl1.x, cl2.x);            
                Console.ReadLine();
            }
        }
    }
    
    Out put will be as below:
    st1 - 100, st2 - 50 cl1 - 50, cl2 - 50
  2. Object of structure store in stack exactly like any other value type like an Integer, a double but object of class store in heap.
  3. Your can assigned null to class variable but you can't assigned null value to structure variable
  4. Class support Inheritance but struct does not support so access modifier of a member of a struct cannot be protected or protected internal
  5. You can use Destructor in a class but You can use it in a structure
  6. When passing a class to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference.
  7. structures can not be garbage collector so no memory management.
    classes can be garbage collector because garbage collector works on heap memory.

Twitter Delicious Facebook Digg Stumbleupon Favorites More