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

  • SEARCH
  • Home
  • 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 9217423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:37:51+00:00 2026-06-18T02:37:51+00:00

Given the following simple console application which illustrates two ways of notifying on changed

  • 0

Given the following simple console application which illustrates two ways of notifying on changed properties:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person(){ Name = "Me" };
            person.Age = 20;
            person.Weight = 80.5F;
                    person.RandomProperty = new RandomComplexObject();

            Console.ReadKey();
        }
    }


    public class Person : BaseObject
    {
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref value, ref _name, false); }
        }

        public int Age
        {
            get { return _age; }
            set { SetProperty<int>(ref value, ref _age, true, "Age", "Weight"); }
        }

        public float Weight
        {
            get { return _weight; }
            set { SetProperty(ref value, ref _weight, true, () => Weight, () => Age); }
        }

        public RandomComplexObject RandomProperty
        {
            get { return _rco; }

            //*** the following line has the error:
            //-------------------------------------
            set { SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight); } 
        }

        private float _weight;
        private int _age;
        private string _name;
        private RandomComplexObject _rco;
    }

    public class BaseObject : INotifyPropertyChanged
    {

        protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                var body = propertyExpression.Body as MemberExpression;
                var expression = body.Expression as ConstantExpression;
                handler(expression.Value, new PropertyChangedEventArgs(body.Member.Name));
            }
        }

        private void OnPropertyChanged(string propertyName)              
        {
            var handler = PropertyChanged;
            if (handler == null)
                return;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetProperty<T>(ref T newValue, ref T currentValue, bool notify, params string[] notifications)
        {
            if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
                return false;

            currentValue = newValue;
            if (notify)
                foreach (var propertyName in notifications)
                    OnPropertyChanged(propertyName);

            return true;
        }

        protected bool SetProperty<T, TProperty>(ref T newValue, ref T currentValue, bool notify, params Expression<Func<TProperty>>[] notifications)
        {
            if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
                return false;

            currentValue = newValue;
            if (notify)
                foreach (var notification in notifications)
                    OnPropertyChanged(notification);

            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

    public class RandomComplexObject{}
}

on the line with the method call SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight); I am experiencing a compilation error:

Cannot convert lambda expression to type ‘string’ because it is not a delegate type

the error shown directly in the IDE is:

The type arguments for method ‘bool ConsoleApplication1.BaseObject.SetProperty(ref T, ref T, bool, params Expression<Func<TProperty>>[])’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

How can I disambiguate this call to the SetProperty() method? Is there a syntactically cleaner way to write this?

  • 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-06-18T02:37:52+00:00Added an answer on June 18, 2026 at 2:37 am

    The following variant at least compiles.

    public RandomComplexObject RandomProperty
    {
        get { return _rco; }
    
        set
        {
            SetProperty(
                ref value,
                ref _rco,
                true,
                () => Name,
                () => Age.ToString(),    //instead of () => Age
                () => Weight.ToString());//instead of () => Weight
        }
    }
    

    The error you’re receving, I guess, was in the first place based on the fact that compiler couldn’t infer the TProperty for

    protected bool SetProperty<T, TProperty>(
        ref T newValue, 
        ref T currentValue, 
        bool notify, 
        params Expression<Func<TProperty>>[] notifications)
    {
        //...
    }
    

    as it’s expecting a variable number of arguments of type Expression<Func<TProperty>> and you passed there lambdas, returning string, int and float. Definitely compiler couldn’t determine, which one of them was TProperty.

    In the setter of the Weight property:

    public float Weight
    {
        get { return _weight; }
        set
        {
            SetProperty(ref value, ref _weight, true, () => Weight, () => Age);
        }
    }
    

    having Weight of type float and Age of type int, compiler inferred TProperty was float, as there is an implicit conversion from int to float.

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

Sidebar

Related Questions

Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
I have a simple console application where I have the following setup: public interface
I am trying to create a simple console based java application, which requires users
Given the following simple BST definition: data Tree x = Empty | Leaf x
A little stuck here. I have a simple question I guess. Given the following
Given a simple table with the following data: id | result | played ----+--------+------------
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
Im developing a simple console application using java. I want to display currently running
Given the following simple TCP socket server running on AppFog that echoes back whatever
I'm using c# 4.0 and a console application just for testing, the following code

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.