2

Is there any way to implement only setter and keep using shorthand (get;) for getter in c# or other way around?

For example: converting email to lower case while setting-

public string Email { get; set { Email = value.ToLower(); } }

Can we do this in any way?

ctor
  • 805
  • 1
  • 10
  • 26
  • Simply put, no you cant. You need to decide between autoimplementation `get;set;` or manually implementing it by using a backingfield. – CSharpie Sep 11 '17 at 12:45
  • Infinite loop on setting Email. You should use a private property variable to transform it. –  Sep 11 '17 at 12:45

1 Answers1

8

No you can't. The auto property syntax creates a hidden field that you cannot access from code, so you cannot set the field even if the syntax above would be valid, which it is not

Shortest version using the latest C# would be

private string _Email     
public string Email { get => _Email; set => _Email = value.ToLower();  }
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357