日期:2014-05-18  浏览次数:20881 次

想使用委托函数代替Console.Readline(),下面的程序错在什么地方?
delegate string readscreen ();
  static void Main(string[] args)
  {
  readscreen readline;
  readline=new readscreen(read);
  string r =Convert.ToString(readline);
  Console.WriteLine("Your words are {0}", r);

  }
  static string read()
  {
  string line = Console.ReadLine();
  return line;
  }

------解决方案--------------------
C# code

  string r =Convert.ToString(readline[color=#FF0000]()[/color]);

------解决方案--------------------
string r =Convert.ToString(readline());
------解决方案--------------------
delegate string readscreen();
static void Main(string[] args)
{
readscreen readline;
readline = new readscreen(read);
string r = Convert.ToString(readline()); Console.WriteLine("Your words are {0}", r);
Console.Read();
}
static string read()
{
string line = Console.ReadLine();
return line;
}
------解决方案--------------------
根据你的意义,应该没有问题:

delegate string ReadEventHandler();

static void Main(string[] args)
{
ReadEventHandler readline=new ReadEventHandler();
Console.WriteLine(readline);
}

static string Read()
{
string line = Console.ReadLine();
return line;
}

以上代码返回:工程名称.Programe+ReadEventHandler;
------解决方案--------------------
 
上面纠正下:ReadEventHandler readline=new ReadEventHandler(Read);

或者如下写:
static void Main(string[] args)
{
 ReadEventHandler readline=delegate()
 {
string line = Console.ReadLine();
return line;
 };
}
OR
static void Main(string[] args)
{
 ReadEventHandler readline=new ReadEventHandler(()=>
 {
string line = Console.ReadLine();
return line;
 });
}
------解决方案--------------------
delegate string readscreen ();

这个委托的使用方法,
readscreen read=new readscreen(target);

调用方法: read();或read.invoke();


------解决方案--------------------


C# code

        /// <summary>
        /// 保存缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SaveCache(object key, object value)
        {
            SaveCacheHandler handler = new SaveCacheHandler(SaveCacheInvoke);
            IAsyncResult ar = handler.BeginInvoke(key, value, new AsyncCallback(SaveCacheCallBack), null);
        }

        private void SaveCacheInvoke(object key, object value)
        {
            lock (hashtable.SyncRoot)
            {
                hashtable[key] = value;
            }
        }

        private void SaveCacheCallBack(IAsyncResult ar)
        {
            SaveCacheHandler handler = (SaveCacheHandler)((AsyncResult)ar).AsyncDelegate;
            handler.EndInvoke(ar);
        }