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 166575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:07:19+00:00 2026-05-11T12:07:19+00:00

Is there any way to access the backing field for a property in order

  • 0

Is there any way to access the backing field for a property in order to do validation, change tracking etc.?

Is something like the following possible? If not is there any plans to have it in .NET 4 / C# 4?

public string Name {     get;     set     {         if (value != <Keyword>)         {             RaiseEvent();         }         <Keyword> = value;     } } 

The main issue I have is that using auto properties doesn’t allow for the same flexibility in validation etc. that a property with a explicit backing field does. However an explicit backing field has the disadvantage in some situations of allowing the class it is contained in to access the backing field when it should be accessing and reusing the validation, change tracking etc. of the property just like any other class that may be accessing the property externally.

In the example above access to the backing field would be scoped to the property thus preventing circumvention of the property validation, change tracking etc.

Edit: I’ve changed < Backing Field > to < Keyword >. I would propose a new keyword similar to value. field would do nicely although I’m sure it’s being used in a lot of existing code.

  • 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. 2026-05-11T12:07:19+00:00Added an answer on May 11, 2026 at 12:07 pm

    Having read your comments in Mehrdad’s answer, I think I understand your problem a bit better.

    It appears that you are concerned about the ability of the developer to access private state in the class they are writing, bypassing your validation logic, etc. This suggests that the state should not be contained in the class at all.

    I would suggest the following strategy. Write a generic class that represents a ValidatedValue. This class holds only the backing value and only allows access/mutation via get and set methods. A delegate is passed to the ValidatedValue to represent the validation logic:

    public class ValidatedValue< T > {     private T m_val;     public ValidationFn m_validationFn;      public delegate bool ValidationFn( T fn );      public ValidatedValue( ValidationFn validationFn )     {         m_validationFn = validationFn;     }      public T get()     {         return m_val;     }      public void set(T v)     {         if (m_validationFn(v))         {             m_val = v;         }     } } 

    You could, of course, add more delegates as required (eg, to support pre/post change notification).

    Your class would now use the ValidatedValue in place of a backing store for your property.

    The example below shows a class, MyClass, with an integer that is validated to be less than 100. Note that the logic to throw an exception is in MyClass, not the ValidatedValue. This allows you to do complex validation rules that depend on other state contained in MyClass. Lambda notation was used to construct the validation delegate – you could have bound to a member function instead.

    public partial class MyClass {     private ValidatedValue<int> m_foo;      public MyClass()     {         m_foo = new ValidatedValue<int>(             v =>              {                 if (v >= 100) RaiseError();                 return true;             }         );     }      private void RaiseError()     {         // Put your logic here....         throw new NotImplementedException();     }      public int Foo     {         get { return m_foo.get(); }         set { m_foo.set(value); }     } } 

    Hope that helps – somewhat off the original topic, but I think it’s more inline with your actual concerns. What we have done is taken the validation logic away from the property and put it on the data, which is exactly where you wanted it.

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

Sidebar

Related Questions

Is there any way to directly access a field of class in the aspx
Is there any way to access a field of a Java class using EL
Is there any way to access file's history using the data in the .svn
Are there any way to access or set iphone's alarm? Im assuming if this
Is there any way to access the owning UITableView from within a UITableViewCell ?
Is there any way to access the iPhone/iPod touch settings programatically ? Thanks. Biranchi
Is there any way to access my asp.net dev server (launched from visual studio)
Is there any way to access the command line arguments, without using the argument
Is there any way to access a tr from an input of a tr
Is there any way to access a movie plaing in VLC through HTTP in

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.