📄 5.3.txt
字号:
Listing 5.3 Allowing Individual Delegate Return Values by Using a Collection
using System;
using System.Collections;
using System.Text;
namespace _6_ReturnValues
{
public delegate bool PrintString( string s, ArrayList retVals );
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList retVals = new ArrayList();
// instantiate delegate
PrintString[] printDelegates = new PrintString[2];
PrintString stringPrinters;
printDelegates[0] = new PrintString( NormalPrint );
printDelegates[1] = new PrintString( FirstCharPrint );
stringPrinters = (PrintString) Delegate.Combine( printDelegates );
// get string from user
Console.Write( “Enter a string: “ );
string input = Console.ReadLine();
stringPrinters( input, retVals );
foreach( string ret in retVals )
{
Console.WriteLine( ret );
}
}
static bool NormalPrint( string s, ArrayList retVals )
{
retVals.Add( s );
return true;
}
static bool FirstCharPrint( string s, ArrayList retVals )
{
StringBuilder retVal = new StringBuilder();
string[] splitStrings = s.Split( new char[]{‘ ‘} );
foreach( string splitString in splitStrings )
{
retVal.Append(splitString[0]);
for( int i = 0; i < splitString.Length; i++ )
retVal.Append( “ “ );
}
retVals.Add( retVal.ToString() );
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -