Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, December 25, 2016

C# Interview Questions and Answers Part 4


Que 1)what is params keyword in C#?
Ans
The "Params" keyword allows passing a variable number arguments to a function.
Using "Params", the arguments passed to a method are changed by the compiler to 
elements in a temporary array, and this array is then used to retrieve the method. 
Generally, you can use params when the number of arguments can vary from 0 to infinity, and use an array when numbers of arguments vary from 1 to infinity.

static public int addTwoEach(int[] args)
    {
        int sum = 0;

        foreach (var item in args)
        {
            sum += item + 2;
        }

        return sum;
    }

addtwoEach(); // throws an error
Compare with params:

static public int addTwoEach(params int[] args)
    {
        int sum = 0;

        foreach (var item in args)
        {
            sum += item + 2;
        }

        return sum;
    }

addtwoEach(); // returns 0 
 
 




Que 2) difference b/w ref and out keyword
Ans
The ref modifier means that:
The value is already set and The method can read and modify it.
The out modifier means that:
The Value isn't set and can't be read by the method until it is set.
The method must set it before returning.
------------------------------------------------------------------------
namespace ConsoleApplication1
{
    class Ref_Out
    {

        public static void Main()
        {
            Ref_Out r = new Ref_Out();
            int a=10;
            int b=20;
            int c=30;
            
            Console.WriteLine(r.mul(ref a, ref b, ref c));

            int d;
            Console.WriteLine(r.mul_out(ref a, out d));
            Console.ReadLine();

        }

        public int mul(ref int a, ref int b, ref int c)
        {
            return a * b * c;
        }



        public int mul_out(ref int a, out int b)
        {
            b = 10;
            d = a * b;
            return d;

        }
    }
}
 
 
Que 3)What is Indexer in C#
Ans
In c# introduce new concept is Indexer. This is very useful for some situation. Let as discuss something about Indexer.
Indexer Concept is object act as an array.
Indexer an object to be indexed in the same way as an array.
Indexer modifier can be private, public, protected or internal.
The return type can be any valid C# types.
Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.
this [Parameter]
{
    get
    {
        // Get codes goes here
    }
    set
    {
        // Set codes goes here
    }
} 
 
For Example:
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Indexers
{
    class ParentClass
    {
        private string[] range = new string[5];
        public string this[int indexrange]
        {
            get
            {
                return range[indexrange];
            }
            set
            {
                range[indexrange] = value;
            }
        }
    }
 
    /* The Above Class just act as array declaration using this pointer */
 
    class childclass
    {
        public static void Main()
        {
            ParentClass obj = new ParentClass();
 
            /* The Above Class ParentClass  create one object name is obj */
 
            obj[0] = "ONE";
            obj[1] = "TWO";
            obj[2] = "THREE";
            obj[3] = "FOUR ";
            obj[4] = "FIVE";
            Console.WriteLine("WELCOME TO C# CORNER HOME PAGE\n");
            Console.WriteLine("\n");
 
            Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);
            Console.WriteLine("\n");
            Console.WriteLine("learning square\n");
            Console.WriteLine("\n");
            Console.ReadLine();
        }
    }
}
 
 
Que 4)  What is an object pool in .NET?
Ans - An object pool is a container having objects ready to be used. It tracks the object that is currently in use, total number of objects in the pool. This reduces the overhead of creating and re-creating objects.
 
Que 5) What is the difference between Finalize() and Dispose() methods?
Ans -  
Finalize:Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed.Internally, it is called by Garbage Collector and cannot be called by user code.It belongs to Object class.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.There is performance costs associated with Finalize method
Dispose:It is used to free unmanaged resources like files, database connections, COM etc. at any time.Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface.It belongs to IDisposable interface.Implement this when you are writing a custom class that will be used by other users.There is no performance costs associated with Dispose method.


Que 6)What is reflection in C#? 
Ans -  
Reflection objects are used for obtaining type information at runtime. 
The classes that give access to the metadata of a running program are in the System.Reflection namespace. 
The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values and objects to the application 
 Reflection provides objects (of typeType) that encapsulate assemblies,modules and types
 You can use reflection to dynamically create an  instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.








You may also like this blog.

1)Abstraction and Encapsulation in OOPS

2)Inheritance in OOPS

3)Polymorphism in OOPS

4)Interface in OOPS

5)What is Virtual Function

6)What is Abstract class and Abstract function

7)What is Static Class and Static Members

8)What is Collections

1) C# Interview Questions and Answers Part3

2)C# Interview Questions and Answers Part 2

No comments :

Post a Comment