[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • 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
 
NavneetCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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