• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

how to do further processing on c# auto-property

let's say i have property like this:

[DataMember]
public string SourceDirectory
        {
            get;
            set;
        }

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.
0
mmingfeilam
Asked:
mmingfeilam
3 Solutions
 
mrjoltcolaCommented:
private string _sourceDirectory;

public string SourceDirectory
        {
            get { return _sourceDirectory; }
            set { if(value != null) _sourceDirectory = value.Trim(); }
        }
0
 
jhshuklaCommented:
Must use normal properties. Although, if you like you can set up validators using attributes.
0
 
Asim NazirCommented:
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: http://msdn.microsoft.com/en-us/library/bb384054.aspx

So if there is additional logic go for old style of properties :)
0
 
NavneetSoftware EngineerCommented:
Hi!

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.


Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now