C# Interview Questions

Showing Questions 21 - 40 of 487 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page:
  •  

    Multicast Delegate

    What is multicast delegate? When it is used?

    Naveen Shivanadri

    • Sep 18th, 2016

    Multicast delegate is useful when the application has many methods (each method for each purpose) which are no return type (void) and those methods are using same parameter. In this case we will use ...

    Reeshabh Choudhary

    • Nov 4th, 2015

    A useful property of delegate objects is that multiple objects can be assigned to one delegate instance by using the + operator. The multicast delegate contains a list of the assigned delegates. When...

  •  

    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.

    Introduction


    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?


    Definition:

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

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

    Declaration:

    public delegate type_of_delegate delegate_name()

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

    Note:
    .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);
    Console.ReadLine();

    }
    }


    Explanation:

    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 .

    delObj(v1,v2);

    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
    }

    }
                               

    Explanation:

    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.

  •  

    What is the difference between an interface and abstract class?

    In the interface all methods must be abstract; in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.

    Naveen Kumar Shivanadri

    • Sep 8th, 2016

    The basic difference is Abstract class can contain abstract methods (a method without method body) as well as concrete methods (a method with method body). Where as in the interface there is only f...

    Ramprit Sahani

    • Sep 1st, 2016

    Interface is an abstract class which has only public abstract methods and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited clas...

  •  

    Find subString in String

    Hi All,
    I have written the below program to find the substring in the string. in case one word coming multiple time how to handle this situation ? the below program retuning -1 as position which is wrong.

    cInput ("abhijit" , "jit")
    public int FindSubString(string strSuper, string strSub)
    {
    char[] charSuper = strSuper.ToCharArray();
    ...

    Annon

    • Jun 15th, 2016

    This answer is wrong. Ex:

    substr("abcabcabcd", "abcabcd")

    => false (should return true)

    Danthe74

    • Dec 14th, 2014

    My solution require only one while loop. Function returns the position of find chars."c# public static int FindSubString(string strSuper, string strSub) { ...

  •  

    Second maximum number

    how to find out second maximum number in C#?

    Roger Hyde

    • Feb 6th, 2016

    Linq makes this quite simple

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

  •  

    Why multiple Inheritance is not possible in C#?

    (Please do not answer like this-It is possible through Interfaces.)

    Anujan

    • Dec 30th, 2015

    I think that ambiguity can be easily overridden by using the class name as a prefix before the method. The same as we do as in case we want to implement a method with the same name as in the case of i...

    Riki

    • Sep 1st, 2015

    C# does not allow multiple inheritance because of ambiguity. Say class A has a property "name" which is inherited by class X and Y now we have another class B which is inheriting both X and Y. Here is...

  •  

    Object Oriented and Object Based Language

    Explain What are Object Oriented Language and Object Based Language

    Star Read Best Answer

    Editorial / Best Answer

    Aarthy_SP  

    • 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...

  •  

    Define reflection

    What is reflection and assembly?

    Poornima

    • Sep 12th, 2012

    Fetching the assembly information at runtime

  •  

    Why strings are immutable?

    Jeevan

    • Apr 20th, 2012

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

  •  

    What is the difference between serialization and encoding?

    Vasyliy Zvarydchuk

    • Sep 5th, 2015

    Encoding is a process of representation some information is optimal format for storing or transmission data. And serialization is representation some object or objects for transmission data between so...

    Hardev Gangwar

    • Jun 3rd, 2015

    1- All three transform data into another format. 2- Both encoding and encryption are reversible. 3- encoding use Algorithm to encode and de-code the data. 4- encryption use Algorithm and key to encryp...

  •  

    Why inheritance is not supported by structures??

    Mohammad Sharib Khan

    • Jan 7th, 2015

    Http://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance

  •  

    Properties in C#

    What are properties in C#? What are the advantages of using Properties ?

    Riki

    • Sep 1st, 2015

    Properties are a level of abstraction to access private fields of a class and they are declare as public. Field are always private member of a class.

  •  

    Does C# support multiple inheritance?

    Skill/Topic: IntermediateA) YesB) No

    Riki

    • Aug 31st, 2015

    Well, C# does not support multiple inheritance of behaviors or properties but we can inherit types implementing interfaces. And because a class can implement multiple interfaces so we can have multipl...

    nyome san

    • Jan 27th, 2015

    No, C# does not support multiple inheritance.However, you can use interfaces to implement multiple inheritance.

  •  

    If A.equals(B) is true then A.getHashcode & B.getHashCode must always return same hash code

    A) TrueB) False

    ViBi

    • Apr 19th, 2015

    Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes

    https://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx

    regex

    • Apr 7th, 2015

    It is clearly false. Proof: "c class MyClass { public int _id; public MyClass(int id) { _id = id; } public bool Equals(MyC...

  •  

    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?

    ViBi

    • Apr 19th, 2015

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

    arupc

    • Feb 8th, 2015

    This is the limitation of Abstract Keyword in C#.

  •  

    String is an

    Skill/Topic: BeginnerA) object typeB) reference type

    Vasu

    • Apr 10th, 2015

    B

    prasad mapari

    • Mar 24th, 2015

    B .?its?reference?type. Bcos?string?dosnt have?default?or?predefined?size. Even?sometimes it?looks?like a?value?type?but?the?actual?gets?stored?in?dynamic?memory?and t?reference?wl b?stored?in?stack.?Thats it

  •  

    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

    LordAlex  

    • 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.

    Vasu

    • Apr 10th, 2015

    Yes

    indrajith

    • Jan 22nd, 2015

    We can use SqlBulkCopy class here for example

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

Showing Questions 21 - 40 of 487 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page: