Shantanu Gupta
Shantanu Gupta

Reputation: 21188

Is it a correct way to use auto-implemented properties in C#

I want to assign some default value to a property or want to replace some character like given below. Is it a correct syntax or should i do this by creating a variable.

 public string Login_Name 
        { 
          get 
            { return this.Login_Name; } 
          set { this.Login_Name = value.Replace("'", "''"); } 
        }

Upvotes: 4

Views: 346

Answers (6)

djdd87
djdd87

Reputation: 68456

By accessing Login_Name the get will return Login_Name again leaving you with an infinite loop (StackOverflowException).

You should use properties to get and set private members:

public string Login_Name
{
   get
   {
      return _login_Name;
   }
   set
   {
      _login_Name = value;
      if (!string.IsNullOrEmpty(_login_Name))
      {
         _login_Name = _login_Name.Replace("'", "''");
      }
   }
}
private string _login_Name;

If you meant to use an auto-implemented property, it would look like this:

public string Login_Name {get;set;}

But auto-implemented properties cannot have any additional logic applied to their gets or sets.

Upvotes: 13

Mark Byers
Mark Byers

Reputation: 837996

What you have written is not an auto-implemented property. An auto-implemented property would look like this:

 public string Login_Name { get; set; } 

Here is a quote from MSDN, emphasis mine:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

When you have extra logic like in your example you cannot use an auto-implemented property. You can use an ordinary property and declare the backing field yourself.

 private string loginName;

 public string LoginName 
 { 
      get
      {
          return loginName;
      } 

      set
      {
          loginName = (value == null) ? null : value.Replace("'", "''");
      } 
 }

Upvotes: 3

Warren Rumak
Warren Rumak

Reputation: 3864

That won't work; you'd effectively be creating an infinite loop.

Use a separate private field instead:

private string m_loginName;

public string Login_Name 
{ 
    get 
    { 
        return m_loginName; 
    } 
    set 
    {
       m_loginName = !string.IsNullOrEmpty(value) ? value.Replace("'", "''") : value;
    } 
}

Upvotes: 4

Andrei Sedoi
Andrei Sedoi

Reputation: 1544

It's correct. The only problem in the snippet that you provided is the recursive call of setter method of the property.

set { this.Login_Name = value.Replace("'", "''"); } 

you should set value to some private field rather than recursively to the property itself, for example:

set { loginName = value.Replace("'", "''"); }

Upvotes: -1

Benny Jobigan
Benny Jobigan

Reputation: 5305

I do it like this.

private string _Login_Name = "Some Default";

public string Login_Name 
{ 
  get { return _Login_Name; } 
  set 
  {  
      _Login_Name = value.Replace("'", "''");  //might want to check for null first
  } 
}

Upvotes: 2

lc.
lc.

Reputation: 116448

You'll have to set a variable, otherwise you'll end up with infinite recursion and a Stack Overflow. With what you have, your setter is calling itself with:

this.Login_Name = ... 

This is in the same way that your getter is calling itself with:

return this.Login_Name;

Upvotes: 1

Related Questions