With the advent of new features like lambda expressions (inline code), does it mean we dont have to use delegates or anonymous methods anymore? In almost all the samples I have seen, it is for rewriting using the new syntax.
Any place where we still have to use delegates and lambda expressions won't work?
...
I have an abstract class and I would like to use it quickly by NOT create a concrete class that inherit the abstract class. Well, to define the abstract method anonymously.
Something like that:
Command c = new Command(myObject){
public override void Do()
{
}
...
Is it possible to unsubscribe an anonymous method from an event?
If I subscribe to an event like this:
void MyMethod()
{
Console.WriteLine("I did it!");
}
MyEvent += MyMethod;
I can un-subscribe like this:
MyEvent -= MyMethod;
But if I subscribe using an anonymous method:
MyEvent += delegate(){Console.WriteLine("I did it!");...
I tried the following code in LINQPad and got the results given below:
List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s)
{
s.Trim();
});
listFromSplit.Dump();
"a" and " b"
so the letter b didn't get the white-space removed as I was expecting...?
A...
I have a simple application with the following code:
FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();
List<Thread> threads = new List<Thread>(files.Length);
foreach (FileInfo f in files)
{
Thread t = new Thread(delegate()
{
Console.WriteLine(f.FullName);
});
thread...
Having a bit of trouble with the syntax where we want to call a delegate anonymously within a Control.Invoke.
We have tried a number of different approaches, all to no avail.
For example:
myControl.Invoke(delegate() { MyMethod(this, new MyEventArgs(someParameter)); });
where someParameter is local to this method
The above will resu...
Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen the examples, and the blog posts regarding anonymous methods, but I don't get them yet. Can someone explain why I should be excited?
...
Once it is compiled, is there a difference between:
delegate { x = 0; }
and
() => { x = 0 }
?
...
I would like to do something like the following:
def add(a, b):
#some code
def subtract(a, b):
#some code
operations = [add, subtract]
operations[0]( 5,3)
operations[1](5,3)
In python, is it possible to assign something like a function pointer?
...
I would like to do something like the following but can't seem to get the syntax for the Do method quite right.
var sqr = new _mocks.CreateRenderer<ShapeRenderer>();
Expect.Call(sqr.CanRender(null)).IgnoreArguments().Do(x =>x.GetType() == typeof(Square)).Repeat.Any();
So basically, I would like to set up the sqr.CanRender() method to ...
Hi,
I know that you cannot return anonymous types from methods but I am wondering how the Select extension method returns an anonymous type. Is it just a compiler trick?
Edit
Suppose L is a List. How does this work?
L.Select(s => new { Name = s })
The return type is IEnumerable<'a> where 'a = new {String Name}
...
Is is possible to have a local variable in an anonymous c# methods, i.e. in the following code I would like to perform the count only once.
IQueryable<Enquiry> linq = db.Enquiries;
if(...) linq = linq.Where(...);
if(...) linq = linq.Where(e =>
(x <= (from p in db.Orders where p.EnquiryId == e.Id select p).Count() &&
(fro...
The following method does not compile. Visual Studio warns "An out parameter may not be used within an anonymous method". The WithReaderLock(Proc action) method takes a delegate void Proc().
public Boolean TryGetValue(TKey key, out TValue value)
{
Boolean got = false;
WithReaderLock(delegate
{
got = dictionary.TryGet...
I'm hoping to clear some things up with anonymous delegates and lambda expressions being used to create a method for event handlers in C#; for myself at least.
Suppose we have an event that adds either an anonymous delegate or lambda expression [for you lucky crowds that can use newer versions of .NET].
SomeClass.SomeEvent += delegate...
As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long.
I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this pr...
Consider the code below:
this.usedIds = 0;
this.SendData = function(data)
{
var id = this.usedIds;
this.usedIds++;
this.xmlHttpThing.open("POST", "/Upload.aspx", true);
this.xmlHttpThing.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var currentObject = this;
this.xmlHttpThing.onreadyst...
I'm looking for the shortest code to create methods to perform common operations on items in an IEnumerable.
For example:
public interface IPupil
{
string Name { get; set; }
int Age { get; set; }
}
Summing a property - e.g. IPupil.Age in IEnumerable<IPupil>
Averaging a property - e.g. IPupil.Age in IEnumerable<IPupil>
Buildi...
The testit() method is a closure. aString has fallen out of scope but testit() can still execute on it. testit2() is using a variable that hasn't fallen out of scope (mystring) but which was also not been passed into testit2(). Is testit2() considered a closure?
string mystring = "hello world";
Action testit = new Action(delegate { st...
So, every time I have written a lambda expression or anonymous method inside a method that I did not get quite right, I am forced to recompile and restart the entire application or unit test framework in order to fix it. This is seriously annoying, and I end up wasting more time than I saved by using these constructs in the first place. ...
C# 2.0 has a neat feature called anonymous functions. This is intended to be used mostly with events:
Button.Click += delegate(System.Object o, System.EventArgs e)
{ System.Windows.Forms.MessageBox.Show("Click!"); };
Now, suppose that Button is a static member, then adding delegates to it would count as unmanaged r...