C# Interview Questions

Showing Questions 1 - 20 of 28 Questions
First | Prev | Next | Last Page
Sort by: 
Jump to Page:

    Describe the accessibility modifier protected internal.

    It is available to derived classes and classes within the same Assembly (and naturally from the base class it is declared in).


    • Jul 29th, 2011

    Protected Internal Mean its a combination of Protected and Internal access modifier, Protected Internal member can be access from same assembly as well as only derived class l in another assembly.


    • Jan 9th, 2006

    Ans:protected internal is available to derived classes and classes within the same Assembly (and naturally from the base class it is declared in). function checkf(form) { var f = document.form1;if (document.form1.cmt.value=='') { alert('Please enter the comment'); return false; }}


    'this' in C#

    Explain about 'this' and where and when it should be used?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Apr-2008 | Apr 3rd, 2008

    The 'this' keyword in C# is used to reference the current class instance.
    It can be optionally used as a qualifier in referencing the fields, properties or methods of the current instance of the class.


    • Apr 3rd, 2008

    The 'this' keyword in C# is used to reference the current class instance.It can be optionally used as a qualifier in referencing the fields, properties or methods of the current instance of the class.


    ByteArray Class

    Write a class called ByteArray that implement allocating, reading and writing to an array of bytes. The runtime environment has a limitation. The maximum continuous memory size that it can allocated is 64k bytes. It can allocate many 64K ( or less) chunks. The ByteArray class should hide this limitation and support allocating arrays larger than 64K as in the following example :ByteArray ba = new ByteArray...


    Reference variable of Interface

    It is possible to create the reference variable of an Interface which is 100% abstract in nature.Then why it is not possible to create a reference of an Abstract class?


    • Apr 19th, 2015

    It is possible "c# class Program { static void Main(string[] args) { MyBase b = new Derived(); Hocky h = new Hocky(); ...


    • Feb 8th, 2015

    This is the limitation of Abstract Keyword in C#.


    How to upload a file in c# console application

    sandya Kolipaka

    • Nov 10th, 2016

    Static void Main(string[] args)
    var wc = new WebClient();
    byte[] response = wc.UploadFile("http:// mysite . com/Tests/Test", "POST", "teste.xml");
    string s = System.Text.Encoding.ASCII.GetString(response);


    • Feb 15th, 2007

    Use System.Net.WebClient Class use UploadFile method provided by the class. It accepts the Uri or string as destination and another parameter as location of file to be uploaded.


    Delegates and Generics in c#

    What is the advantage of using delegates and Generics in c# .

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Dec-2007 | Apr 18th, 2008

    Advantage of Generic:-

    1. Generics provide type safety without the overhead of multiple implementations.
    Ex. We can create a linked list of string.
        LinkedList<string>linkList=new LinkedList<string>();
    There is no need to inherit from a base type and override members.The linked list is ready

    for immediate use.

    2. There is no need to write code to test for the correct data type because it is enforced

    at compile time. The need for type casting and the possibility of run-time errors are


    3. Generic collection types generally perform better for storing and manipulating value

    types because there is no need to box the value types.

    4. Generic delegates enable type-safe callbacks without the need to create multiple delegate


    5.Generic delegates can also be used in dynamically generated code without requiring the

    generation of a delegate type. This increases the number of scenarios in which you can use

    lightweight dynamic methods instead of generating entire assemblies.

    Advantage of Delegate:

    Delegates are managed function pointers. they are type checked and held in spaces that can

    be reclaimed by the memory manager.


    C# Calender Days

    How to find out how many calender days are there between two dates in c#.net?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since May-2008 | May 13th, 2008

    DateTime t1 = new DateTime(2008, 5, 13);

    DateTime t2 = new DateTime(2004, 2, 22);

    TimeSpan span = (t1 > t2 ? t1 - t2 : t2 - t1);



    • Nov 4th, 2009

    Here is the better answer:DateTime fDate = DateTime.Now;int thisDay = fDate.DayOfYear; //if today's date is 31/12/2008, thisDay will be 365 and if today's date is 01/01/2009, thisDay will be 1.Therefore, you can use DayOfYear propery to get the day number in a year.


    Advantages of static class over class ?

    When to use static class and when to use normal class ?


    • Aug 31st, 2014

    WHEN SHOULD WE DO THIS: When we have a normal class and its methods being used very frequently in a given app, then it would speed up things if we made this class a static class with static methods. T...


    • Aug 19th, 2014

    A static class can make your implementation simpler and faster because you do not have to create an object in order to invoke its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.


    How do I create a Delegate/MulticastDelegate?

    C# requires only a single parameter for delegates: the method address. Unlike other languages, where the programmer must specify an object reference and the method to invoke, C# can infer both pieces of information by just specifying the method's name. For example, let's use System.Threading.ThreadStart: Foo MyFoo = new Foo();ThreadStart del = new ThreadStart(MyFoo.Baz);This means that delegates can...

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: Raghu

    • Sep 29th, 2005

    This article is good.


    In this article I am going to share my knowledge on Delegates in C#.This would explain the Delegate using simple examples so that the beginner can understand the same.

    What is Delegate?


    Delegate is type which  holds the method(s) reference in an object.
    it is also reffered as a type safe function pointers.

    .Encapsulating the method's call from caller
    .Effective use of Delegat improves the performance of application.
    .used to call a method asynchronously.


    public delegate type_of_delegate delegate_name()

    Example : public delegate int mydelegate(int delvar1,int delvar2)

    .you can use delegeate without parameter or with parameter list
    .you should follow the same syntax as in the method
    (if you are reffering the method with two int parameters and int return type the delegate which you are declaring should be the same format.This is how it
    is reffered as type safe function pointer)

    Sample Program using Delegate :

    public delegate double Delegate_Prod(int a,int b);

    class Class1

    static double fn_Prodvalues(int val1,int val2)
    return val1*val2;
    static void Main(string[] args)

    //Creating the Delegate Instance
    Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);

    Console.Write("Please Enter Values");

    int v1 = Int32.Parse(Console.ReadLine());
    int v2 = Int32.Parse(Console.ReadLine());

    //use a delegate for processing

    double res = delObj(v1,v2);
    Console.WriteLine ("Result :"+res);



    Here I have used a small program which demonstrates the use of delegate.

    The delegate "Delegate_Prod" is declared with double return type and which accepts only two integer parameters.

    Inside the Class the method named fn_Prodvalues is defined with double return type and two integer parameters.(The delegate and method is having the same signature and parameters type)

    Inside the Main method the delegate instance is created and the function name is passed to the
    delegate instance as following.

    Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);

    After this we are accepting the two values from the user and passing those values to the delegate as we do using method .


    Here  delegate object encapsulates the method functionalities and return the result as we specified
    in the method.

    Multicast Delegate

    What is Multicast Delegate? :
    It is a Delegate which holds the reference of more than one methods.
    Multicast delegates must contain only methods that return void, else there is a run-time exception.

    Simple Program using Multicast Delegate

    delegate void Delegate_Multicast(int x, int y);

    Class Class2

    static void Method1(int x, int y) {
     Console.WriteLine("You r in Method 1");
    static void Method2(int x, int y) {
     Console.WriteLine("You r in Method 2");
    public static void Main()
     Delegate_Multicast func = new Delegate_Multicast(Method1);

     func += new Delegate_Multicast(Method2);
     func(1,2);             // Method1 and Method2 are called
     func -= new Delegate_Multicast(Method1);
     func(2,3);             // Only Method2 is called



    In the above example you can see that two methods are defined named method1 and method2 which takes two integer parameters and return type as void.

    In the main method the Delegate object is created using the following statement

    Delegate_Multicast func = new Delegate_Multicast(Method1);

    Then the Delegate is added using the += operator and removed using -= operator.

    Naveen Kumar Shivanadri

    • Sep 8th, 2016

    Delegate is not type. It is just pointer method which is used to improve the performance. Suppose we want to execute a method 10 times by using calling method by using class name or instance, 10 time...

    Rajesh Muurya

    • Aug 9th, 2016

    I have some doubt.
    Raghus Says: Delegate is type which holds the method(s) reference in an object.
    My question is if Delegate is type then how we create a object of delegate? Because, we know that we can create object only struct and class.


    Following are the collections in C#:

    Skill/Topic: AdvancedA) structsB) enumC) dictionariesExplanation: Ans. b & c. a isn’t a collection .

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Nov-2011 | Nov 5th, 2011

    All collections directly or indirectly should implement the ICollection interface or the ICollection(Of T) generic interface. If you check System.Array you will see ICollection interface in there. Correct answer is C, it doesn't matter that there are methods that can provide arrays of Names and Values. Because if you are reasoning like this, it means bject.ToString().ToCharArray() cause all classes are being collections.


    • Nov 5th, 2011

    All collections directly or indirectly should implement the ICollection interface or the ICollection(Of T) generic interface. If you check System.Array you will see ICollection interface in there. Co...


    Advantage of avl tree over binary search tree.

    What is advantage using avl tree instead of using binary search tree ?


    • Nov 23rd, 2016

    In BST, the time complexity of search operation (average case) is taken to be O(log n). But in the worst case, i.e the degenerate trees/skewed trees time complexity of search operation is O(n) which c...


    • Sep 8th, 2015

    Better search times for keys


    The following code fails. Why?

    int a = 5; int b = 5; object oa = a; object ob = b; Debug.Assert(oa == ob, "oa is not equal ob");


    • Oct 17th, 2013

    Given code fails bcoz. debug result is wrong.. Please see below int a = 5; int b = 5; object oa = a; object ob = b; Debug.Assert(oa != ob...


    Second maximum number

    how to find out second maximum number in C#?

    Roger Hyde

    • Feb 6th, 2016

    Linq makes this quite simple

    1. int GetSecondMax(int[] numbers)
    2. {
    3.         return numbers.Distinct().OrderByDescending(n => n).Skip(1).Take(1).FirstOrDefault();
    4. }


    Object Oriented and Object Based Language

    Explain What are Object Oriented Language and Object Based Language

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Dec-2008 | Dec 10th, 2008

    Any langauge based on encapsulation concpet and operations with the objects are called as Object Based language. Exmaple : VB is a Object based and not an Object oriented

    Any langauge based on encapsulation concept and operations with the objects and also dealing with the inheritance and polymorphism are called as Object Oriented language. Exmaple : C++,C#

    Arpit Mandloi

    • Nov 25th, 2015

    Object Based languages: 1. Object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance 2. Object-based language has in-built object like JavaScript has window obje...


    What is the difference between const and static read-only?

    The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it:-- in the variable declaration (through a variable initializer).-- in the static constructor (instance constructors if it's not static).

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: Eddie Quiroz

    • Sep 3rd, 2005

    A const must be initialized at the time of its creation. A readonly field can be assigned to once in the class constructor allowing you to pass in the value at run-time. Declaring fields as const protects both you and other programmers from accidentally changing the value of the field. Also note that with const fields, the compiler performs some optimization by not declaring any stack space for the field. The readonly keyword is similar to const, with two exceptions. First, the storage of a readonly field is the same as a regular read-write field, and thus there is no performance benefit. Secondly, readonly fields can be initialized in the constructor of the containing class.


    • Apr 7th, 2009

    As static read-only variables must be initialized in the static constructor (static constructor cannot have parameters and it cannot be called manually), it is efficient to used const variables over static read-only variables if you know the values at creation of these variables.


    Public Members

    Why do we use properties rather than public members?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Oct-2009 | Oct 9th, 2009

    To the user of an object, property and public member may not make much difference with respect to syntax. However, property provides the ability to combine both field and method via the get and set accessors.

    MS Help for C# presents the best answer for using properties:

    Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.



    • Apr 1st, 2010

    With Properties, we can implement validation.For example, if we have customer age and we want to add validation on  the age, we can do it with properties.Another benefits of properties is that we can protect a field in a class by reading and writing it through the properties.


    • Mar 7th, 2010

    Private data fields of class should not be exposed by making them public. Instead they can made visible using public properties in more controlled manner. Also we can make properties read only or read/write.


    AutoPostBack Property

    There is Cancel button and OK button on page. When we click Cancel button it skips all validation controls and goes to next page.(a)Page AutoPostback of Cancel is True.(b)Page AutoPostback of Cancel is False.(c)Page AutoPostback of OK is True.(d)Page AutoPostback of OK is False.Which option is correct?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Dec-2009 | Dec 10th, 2009

    A button control does not have AutoPostBack property it has click event. so once you click on cancel and if its CausesValidation Property is false it will skips all validations, and if it is true it will validate the controls on the page.

    AutoPostBack property is for say DropDown,textbox etc... whnever you change/enter value/keyed some caharcter in textbox or change your selection in drop down postback will happen if that control's is AutoPostBack property is true, depending on that postback you can fill values or set some values for other controls or you can do some validation against enterd or selcted values.

    e.g. say tehre are 2 drop down. DropDown1 and DropDown2
    DropDown2 is currently holding states of US country as in DropDown1 you have selcted US. Now you want to fill DropDown2 with state value of India.
    Now if
    DropDown1.AutopostBack = True;

    and you change DropDown1's value from Us to India. 
    Page will post back to server and you can fill DropDown2 with Indian States.



    C# Data Table Multiple Rows

    You have multiple rows in C# data table and you have to save it in database using just one call, How will you do it?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Nov-2011 | Nov 5th, 2011

    In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to: 1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter. 2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects. 3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data. 4. Call the Update method of the DataAdapter, passing the second DataSet as an argument. 5. Invoke the Merge method to merge the changes from the second DataSet into the first. 6. Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.


    • Apr 10th, 2015



    • Jan 22nd, 2015

    We can use SqlBulkCopy class here for example

    1. SqlBulkCopy  bulkCopy=new SqlBulkCopy  (sqlconn);
    2.  bulkCopy.DestinationTableName = "dbtablename";
    3.  bulkCopy.WriteToServer(datatable);


    What are constructors in .Net

    Abinash Dinakaran

    • Dec 22nd, 2014

    Constructors are the member functions which has same name as class name. Constructors are used to initialize a value for a variable so that we can be able to avoid junk values thrown to a variable.


    • Sep 24th, 2014

    Constructor is the special type of methods, which is called when we crate the object for the class.
    Constructor will have same name as class name.
    overloaded constructor can also be there.


    Why strings are immutable?


    • Apr 20th, 2012

    People, please read the question before answering, don't just answer "Why" is the question - not "how".

Showing Questions 1 - 20 of 28 Questions
First | Prev | Next | Last Page
Sort by: 
Jump to Page: