Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I came across this example and the output is 101. But dont understand why.

 

Anyone can give me a brief explanation?

 

using System;
class MainClass
{
delegate void PtrToMethod( string str, params object [] args );
static void Main( )
{
	    PtrToMethod PtrToWriteLine = new PtrToMethod( Console.WriteLine );
	    PtrToWriteLine( "{1}", 100, 101, 102 );	    
}
}

Posted

You have to understand how the String.Format method works.

 

In your example, an array of objects are passed in as a second parameter. The array contains the values 100, 101, 102. Because they are integers, the values are boxed. In your 1st parameter, a string, you can insert/access the value of the array by using the format '{' + index + '}'. This is done by using the ToString method.

 

"{0}" would output 100.

"{1}" would output 101.

"{2}" would output 102.

"{3}" would thrown an exception.

  • Administrators
Posted

If you remove all the delegate related stuff and was just to use

Console.WriteLine("{1}", 100, 101, 102);

You would be getting the output because the Writeline method takes a string followed by a number of parameters - the {1} indicates that it should display the 2nd parameter following the string (0 based offset) - if you haven't come across this before then I suggest you read up on the String.Format() method.

 

The more confusing aspect of this is probably the delegate related stuff.

 

the line

delegate void PtrToMethod( string str, params object [] args );

 

defines a delegate, a delegate can be thought of as a 'Function Pointer'; in otherwords it isn't a method itself but at runtime we can assign a 'real' method to it; then we can call it as if it was a function. Delegates are always strongly typed - the one in your example is compatible with any method that takes a single string and a variable number of objects (just happens to be the same signature as Console.WriteLine)

 

the line

PtrToMethod PtrToWriteLine = new PtrToMethod( Console.WriteLine );

creates a new instance of the delegate which points to the Console.WriteLine method, from now on we can call PtrToWriteLine as if it was a normal method and behind the scenes it will invoke the Console.WriteLine method for us.

The real power of this is that we could use an alternate method of display by changing what PtrToWriteLine points to at runtime.

 

e.g.

using System;
class MainClass
{
delegate void PtrToMethod( string str, params object [] args );
static void Main( )
{
PtrToMethod PtrToWriteLine = new PtrToMethod( Console.WriteLine );
PtrToWriteLine( "{1}", 100, 101, 102 );	    
     	PtrToMethod PtrToWriteLine = new PtrToMethod(this.TestMethod);
PtrToWriteLine( "{1}", 100, 101, 102 );	    
}
       
       void TestMethod(string s, params object [] stuff)
       {
       Console.WriteLine("Second Sample {1}, more Text {0});
       }
}

The above code uses the original, then swaps the delegate to point to the TestMethod which changes how it displays the data, notice in both cases PtrToWriteLine is called in the same way.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...