dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [DotGNU].net futures


From: Thong (Tum) Nguyen
Subject: RE: [DotGNU].net futures
Date: Sat, 23 Aug 2003 12:16:11 +1200

I noticed that too.  I wonder if they’ll allow explicitly casting between two different but compatible delegates.

 

e.g.

 

delegate void D1(string s);

delegate void D2(string s);

 

D1 d1;

D2 d2

 

d1 = delegate(string s) { };

d2 = (D2)d1;

 

Of course you could just use:

 

d2 = delegate(string s) { d1(s) };

 

An implicit conversion without a cast would be naughty but seeing as they’re relaxing the requirement to explicitly construct the delegate from its type it makes sense to allow casting from between compatible delegates (even if the cast is really just a disguised proxy anonymous method).

^Tum

-----Original Message-----
From:
Neil Cawse [mailto:address@hidden.com]
Sent: Saturday, 23
August 2003 11:40 a.m.
To:
address@hidden.org
Subject: [DotGNU]
.net futures

 

 

I've noticed several people referencing this summary of new C# features over on GDN lately in response to the recent flurry of discussion on delegates.

 

Readers should be aware that the syntax for anonymous methods has changed somewhat since that whitepaper was posted. Specifically, one no longer uses the name of the delegate type when declaring the anonymous method.

 

For example, this code excerpted from the aforementioned whitepaper:

 

public MyForm()

{
      listBox = new ListBox(...);
      textBox = new TextBox(...);
      button = new Button(...);
      button.Click +=
new EventHandler(sender, e)
      {
      listBox.Items.Add(textBox.Text);
      };
   }
}

 

would now be written like this:

 

public MyForm()
{
      listBox = new ListBox(...);
      textBox = new TextBox(...);
      button = new Button(...);
      button.Click +=
delegate(object sender, EventArgs e)
      {
      listBox.Items.Add(textBox.Text);
      };
   }
}

 

It's also worth noting that this example illustrates two new features. The obvious one is the anonymous method feature. The more subtle feature is the fact that Whidbey C# relaxes the requirement to explicitly instantiate a delegate type. For example, consider the first example (not using anonymous methods):

 

public MyForm()

{
      listBox = new ListBox(...);
      textBox = new TextBox(...);
      button = new Button(...);
      button.Click +=
new EventHandler(AddClick);
   }
}

 

In whidbey C#, you are allowed to (but not required to) omit the "new T" and just do the assignment:

 

public MyForm()
{
      listBox = new ListBox(...);
      textBox = new TextBox(...);
      button = new Button(...);
      button.Click +=
AddClick;
   }
}

 

Shades of C function pointers!

 

Of course, readers should be aware that the moment after I click the submit button all of this might change yet again (unlikely but not impossible).

 

As always, consult the actual bits (which can be had at PDC in October).


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.298 / Virus Database: 161 - Release Date: 13/11/2001


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.298 / Virus Database: 161 - Release Date: 13/11/2001


reply via email to

[Prev in Thread] Current Thread [Next in Thread]