Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3799122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:40:59+00:00 2026-05-19T13:40:59+00:00

(with C# 3.0 and VS 2008). Doing MVVM WPF stuff you often write properties

  • 0

(with C# 3.0 and VS 2008).

Doing MVVM WPF stuff you often write properties like this:

public bool MyProperty {
    get{return _myProperty;}
    set{
        if(_myProperty == value)return;
        _myProperty = value;
        RaisePropertyChanged("MyProperty");
    }
}

Doing TDD I often end up writing tests such as:

[Test]
public void MyPropertyRaisesPropertyChangedWhenChanged(){
    var mySUT = CreateSUT();

    bool eventRaised = false;
    string propName = "";

    mySUT.PropertyChanged += 
        (s,e)=>{eventRaised = true;propName = e.PropertyName;};

    Assert.That(mySUT.MyProperty,Is.False(),"mySUT.MyProperty");

    mySUT.MyProperty = true;

    Assert.That(eventRaised,"eventRaised");
    Assert.That(propName, Is.EqualTo("MyProperty"),"propName");

    // could check not raised when set same...
}

I experimented with a method like this:

public class MyTestMethods{

    public static PropertyChangedEventHandler MakePropertyChangedHandler(
        bool eventWasRaised, string propertyName){    

        return (s,e)=>{eventWasRaised = true; propertyName = e.PropertyName};

    }
} 

So that I could write my test:

[Test]
    public void MyPropertyRaisesPropertyChangedWhenChanged(){
        var mySUT = CreateSUT();

        bool eventRaised = false;
        string propName = "";

        mySUT.PropertyChanged += 
            MyTestMethods.MakePropertyChangedHandler(eventRaised,propName);

        // etc...
}

But VS2008 told me that eventRaised would always be false.

I thought maybe changing MakePropertyChangedHandler to use ref parameters would work

    public static PropertyChangedEventHandler MakePropertyChangedHandler(
        ref bool eventWasRaised, ref string propertyName){

        return // lambda...

    }

but VisualStudio tells me ‘Cannot use ref or out parameter ‘x’ inside an anonymous method body’.

Can anyone tell me if it’s possible to write a working method like MakePropertyChangedHandler and if not, how come?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:41:00+00:00Added an answer on May 19, 2026 at 1:41 pm

    It’s not possible to give ref to a lambda, because proper life cycle management can’t be ensured. When the compiler encounters a closure (lambda using outer scope variable), it

    1. wraps all captured local variables in an anonymous object,
    2. creates instance of this object instead of allocating the variables on the stack,
    3. makes the lambda code a method of this object and
    4. returns a delegate to this object and method

    (details might be a bit different, but that’s the principle). This way the captured variables exist as long as the delegate does.

    However when compiling the function higher up the stack, the compiler does not know this and so it allocates variables on the stack. That’s faster, but the variables only exist until the function returns. Since the closure can live longer than that (in your case it won’t, but the compiler can’t know), the closure can’t refer to the stack variable.

    What you can do is create an object with reference semantics (which lives as long as it’s refered to) and give it to the closure. So if you create:

    class BoolHolder {
        public bool value;
    };
    

    pass BoolHolder to the lambda and in the lambda do

    boolHolder.value = true;
    

    than you’ll see the change outside.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some tests on a Windows Server 2008. When I try to access
We're doing windows mobile development using the WM6 Professional emulator through visual studio 2008
Now that I've finnaly moved to doing some development/support work for Windows 2008 I
I'm looking for a component for C#.NET 2008 Professional that is capable of doing
When building a VS 2008 solution with 19 projects I sometimes get: The GenerateResource
VB 2008. I have several text boxes on a form and I want each
Server 2008 machine with SP1, Visual Studio 2008 with SP1. My user; Developer, is
Does SQL Server 2008 ship with the .NET 3.5 CLR, so that stored procedures
It's fall of 2008, and I still hear developers say that you should not
I normally run VS 2008 at home and LINQ is built in. At work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.