how to do further processing on c# auto-property

Posted on 2011-04-29
Last Modified: 2012-06-27
let's say i have property like this:

public string SourceDirectory

but i want to further process the value during set so that it gets trimmed, is there a way to do that or must i use normal properties with a internal field?  also, these are properties that are part of a XML serializable class.
Question by:mmingfeilam
    LVL 40

    Expert Comment

    private string _sourceDirectory;

    public string SourceDirectory
                get { return _sourceDirectory; }
                set { if(value != null) _sourceDirectory = value.Trim(); }
    LVL 9

    Accepted Solution

    Must use normal properties. Although, if you like you can set up validators using attributes.
    LVL 10

    Assisted Solution

    by:Asim Nazir
    As per MSDN:
    In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.
    You can read further at:

    So if there is additional logic go for old style of properties :)
    LVL 15

    Assisted Solution


    C# auto-property simply creates field in background and does no other magic.
    Hence if yo want to access the that variable and want to apply some logic around it's clear enough not to use auto-properties.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now