Write some code to use a delegate. 

1 Answer

Answer :

Member function with a parameter

using System;

namespace Console1

{

class Class1

{

delegate void myDelegate(int parameter1);

static void Main(string[] args)

{

MyClass myInstance = new MyClass();

myDelegate d = new myDelegate(myInstance.AMethod);

d(1); // <--- Calling function without knowing its name.

Test2(d);

Console.ReadLine();

}

static void Test2(myDelegate d)

{

d(2); // <--- Calling function without knowing its name.

}

}

class MyClass

{

public void AMethod(int param1)

{

Console.WriteLine(param1);

}

}

}

Multicast delegate calling static and member functions

using System;

namespace Console1

{

class Class1

{

delegate void myDelegate(int parameter1);

static void AStaticMethod(int param1)

{

Console.WriteLine(param1);

}

static void Main(string[] args)

{

MyClass myInstance = new MyClass();

myDelegate d = null;

d += new myDelegate(myInstance.AMethod);

d += new myDelegate(AStaticMethod);

d(1); //both functions will be run.

Console.ReadLine();

}

}

class MyClass

{

public void AMethod(int param1)

{

Console.WriteLine(param1);

}

}

}


Related questions

Description : Explain what a delegate is and how one works. 

Last Answer : A delegate acts like a strongly typed function pointer. Delegates can invoke the methods that they reference without making explicit calls to those methods.

Description : What’s a multicast delegate?

Last Answer : A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called. 

Description : What’s a delegate? 

Last Answer : A delegate object encapsulates a reference to a method. 

Description : What is a delegate useful for?

Last Answer : The main reason we use delegates is for use in event driven programming. 

Description : What is a delegate? 

Last Answer : A delegate in C# is like a function pointer in C or C++. A delegate is a variable that calls a method indirectly, without knowing its name. Delegates can point to static or/and member functions. It is also possible to use a multicast delegate to point to multiple functions.

Description : What’s a multicast delegate?

Last Answer : A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called. 

Description : What’s a delegate? 

Last Answer : A delegate object encapsulates a reference to a method. 

Description : Define what is a multicast delegate?

Last Answer : Each delegate object holds a reference to a single method. However, it is possible for a delegate object to hold references and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.

Description : Write some code to declare and use properties.

Last Answer : // instance public string InstancePr { get { return a; } set { a = value; } } //read-only static public static int ClassPr { get { return b; } }

Description : Write some code that declares an array on ints, assigns the values: 0,1,2,5,7,8,11 to that array and use a foreach to do something with those values.

Last Answer : int x = 0, y = 0; int[] arr = new int [] {0,1,2}; foreach (int i in arr) { if (i%2 == 0) x++; else y++; }

Description : Write code to use threading and the lock keyword. 

Last Answer : using System; using System.Threading; namespace ConsoleApplication4 { class Class1 { [STAThread] static void Main(string[] args) { ThreadClass tc1 = new ThreadClass(1); ThreadClass tc2 = new ThreadClass( ... Console.WriteLine(threadNumber + " working"); Thread.Sleep(1000); } } } } }

Description : Write code to define and use your own custom attribute.

Last Answer : (From MSDN) // cs_attributes_retr.cs using System; [AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple=true)] public class Author : Attribute { public Author(string name) { this.name ... double version; string name; public string GetName() { return name; } }

Description : Write some code to implement an indexer.

Last Answer : using System; namespace Console1 { class Class1 { static void Main(string[] args) { MyIndexableClass m = new MyIndexableClass(); Console.WriteLine(m[0]); Console.WriteLine(m[1]); Console.WriteLine(m[2]); Console. ... get { return myData[i]; } set { myData[i] = value; } } } }

Description : Write some code that uses an ArrayList.

Last Answer : ArrayList list = new ArrayList(); list.Add("Hello"); list.Add("World");

Description : Write some code to implement a jagged array. 

Last Answer : // Declare the array of two elements: int[][] myArray = new int[2][]; // Initialize the elements: myArray[0] = new int[5] {1,3,5,7,9}; myArray[1] = new int[4] {2,4,6,8};

Description : Write some code to implement a multidimensional array.

Last Answer : int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}; 

Description : Write some code for a custom collection class.

Last Answer : using System; using System.Collections; public class Items : IEnumerable { private string[] contents; public Items(string[] contents) { this.contents = contents; } public IEnumerator GetEnumerator() { ... (string item in items) { Console.WriteLine(item); } Console.ReadLine(); } }

Description : Write some code for do… while.

Last Answer : int x; int y = 0; do { x = y++; Console.WriteLine(x); } while(y < 5);

Description : Write some code for a while loop. 

Last Answer : int n = 1; while (n < 6) { Console.WriteLine("Current value of n is {0}", n); n++; }

Description : Write some code for a for loop 

Last Answer : for (int i = 1; i <= 5; i++) Console.WriteLine(i);

Description : Write some if… else if… code.

Last Answer : int n=4; if (n==1)  Console.WriteLine("n=1"); else if (n==2) Console.WriteLine("n=2"); else if (n==3) Console.WriteLine("n=3"); else Console.WriteLine("n>3");

Description : Write some try…catch…finally code.

Last Answer : // try-catch-finally using System; public class TCFClass { public static void Main () { try { throw new NullReferenceException(); } catch(NullReferenceException e) { Console.WriteLine("{0} exception 1 ... ("exception 2."); } finally { Console.WriteLine("finally block."); } } }

Description : Write some code to overload an operator. 

Last Answer : class TempleCompare { public int templeCompareID; public int templeValue; public static bool operator == (TempleCompare x, TempleCompare y) { return (x.templeValue == y.templeValue); } ... templeValue); } public override int GetHashCode() { return templeCompareID; } }

Description : Write some code to box and unbox a value type. 

Last Answer : // Boxing int i = 4; object o = i; // Unboxing i = (int) o;

Description : Write some code using interfaces, virtual methods, and an abstract class.

Last Answer : using System; public interface Iexample1 { int MyMethod1(); } public interface Iexample2 { int MyMethod2(); } public abstract class ABSExample : Iexample1, Iexample2 { public ABSExample( ... override void VIRTMethod2() { System.Console.WriteLine("VIRTMethod2 has been overridden"); } }

Description : Write code for a case statement. 

Last Answer : switch (n) { case 1: x=1; break; case 2: x=2; break; default: goto case 1; }

Description : Write code for an enumeration. 

Last Answer : public enum animals {Dog=1,Cat,Bear}; 

Description : Write code to show how a method can accept a varying number of parameters.

Last Answer : using System; namespace Console1 { class Class1 { static void Main(string[] args) { ParamsMethod(1,"example"); ParamsMethod(1,2,3,4); Console.ReadLine(); } static void ... o in list) { Console.WriteLine(o.ToString()); } } } }

Description : Describe how to use code to retrieve resources at run time.

Last Answer : You must first create an instance of the ResourceManager class that is associated with the assembly that contains the desired resource. You can then use the GetString method to retrieve string resources or the GetObject method to retrieve object resources.

Description : Briefly highlight the differences between imperative and declarative security as they pertain to code access security.

Last Answer : Imperative security is implemented by calling methods of Permission objects in code at run time. Declarative security is configured by attaching attributes representing permissions to classes ... request assembly-wide permissions using the Assembly (assembly) directive with declarative security.

Description : Explain how to convert data in legacy code page formats to the Unicode format.

Last Answer : You can use the Encoding.Convert method to convert data between encoding types. This method requires instances of both encoding types and an array of bytes that represents the data to be converted. ... GetBytes method and can convert an array of bytes back to chars with the Encoding.GetChars method.

Description : Describe how to retrieve the ASCII key code from a keystroke. How would you retrieve key combinations for non-ASCII keys?

Last Answer : Keystrokes can be intercepted by handling the KeyPress and KeyDown events. Both of these events relay information to their handling methods in the form of their EventArgs. The KeyPressEventArgs, ... you would handle the KeyDown event and use the properties exposed by the KeyEventArgs instance.

Description : What is unsafe code?

Last Answer : Unsafe code bypasses type safety and memory management.  

Description : Why is the virtual keyword used in code?

Last Answer : The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.

Description : Define what is a code group?

Last Answer : A code group is a set of assemblies that share a security context.

Description : How would you read and write using the console?

Last Answer : Console.Write, Console.WriteLine, Console.Readline 

Description : Write a hello world console application. 

Last Answer : using System; namespace Console1 { class Class1 { [STAThread] // No longer needed static void Main(string[] args) { Console.WriteLine("Hello world"); } } }

Description : C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? 

Last Answer : Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

Description : What does to delegate mean ?

Last Answer : answer:As a verb, to delegate means to assign a job or task or job function to a subordinate. As a noun, a delegate is someone who represents a group of people to a governing body.

Description : When did people first start to delegate authority?

Last Answer : It started as soon as there were two people. There have always been leaders and followers (and people who get in the way).

Description : What person was NOT a delegate to the constitutional convention?

Last Answer : This is an unusual question - there were a great many people whowere NOT delegates. A better question would be "Who were thedelegates?"

Description : What are the basic reasons why supervisors fail to delegate as much as they should?

Last Answer : Need answer

Description : What delegate from Virginia had a plan for a strong central government?

Last Answer : Need answer

Description : Which statement best describes a responsibility of the project manager: a. to be the sole source of expertise for estimating techniques on cost and time. b. to deliver the project objectives to ... benefits. d. to delegate all accountability for managing time, cost and quality to team leaders.

Last Answer : b. to deliver the project objectives to enable benefits to be realised.

Description : Which statement best describes a responsibility of the project manager: a. to be the sole source of expertise for estimating techniques on cost and time.  b. to deliver the project objectives to ... benefits.  d. to delegate all accountability for managing time, cost and quality to team leaders.

Last Answer : b. to deliver the project objectives to enable benefits to be realised.

Description : From the following action , which action is not re delegate to subordinate authorities ? a . action related with creation of Post. b. appointing a government servant c. issue order related with changes of allowance

Last Answer : . action related with creation of Post.

Description : How do you decide what to delegate and to whom?

Last Answer : Identify the strengths of your team members and their From there, invest the tasks upon each member based on where you think you'll get the best return.

Description : When top -level managers delegate very little authority to lower -level employees, the organisation is : A)centralised B)decentralised C)empowered D)marketing -oriented

Last Answer : D)marketing -oriented

Description : Explain how to use the PrintPreviewDialog control to display a preview of a printed document before it is printed.

Last Answer : You display a preview of a printed document with the PrintPreviewDialog control by first setting the PrintDocument property of the PrintPreviewDialog instance to the PrintDocument you want to preview, then by calling the PrintPreviewDialog.Show command to display the dialog box. 

Description : Explain how to use the HelpProvider component to provide help for UI elements.

Last Answer : You can provide either a HelpString or a help topic for UI elements with the HelpProvider. The HelpProvider provides HelpString, HelpKeyWord, and HelpNavigator properties for each control on the form. If ... particular element is displayed when the element has the focus and the F1 key is pressed.