Auto Property Default Return

Topics: C# Language Design
Sep 14, 2014 at 2:14 PM
Hi Team,

Great job done with Roslyn and the new C# 6 features and language enhancements.

I really like the new auto property initializers.
However, I would like to suggest an extension to that or slightly new language enhancement.
A feature to allow us not just initialize the auto properties, but specify the default return value as well.

In other words, in addition to:
public class Person
{
     public string First { get; set; } = "Jane";
     public string Last { get; set;} = "Doe";
} 
I would like to be able to specify what is the default value to be returned by a property, when it is or it is set to NULL:
public class Person
{
     public string First { get; set; } ?? "Jane";
     public string Last { get; set;} ?? "Doe";
} 
Hope this makes any sense to you as well.

Keep the good work.
Best Regards
Sep 14, 2014 at 5:33 PM
Edited Sep 14, 2014 at 5:36 PM
Hey,

Some more thoughts for auto properties:
What about lazy initialization enhancement of auto properties and instead of:
public string FirstName 
{
    get 
    {
        if(_firstName == null) 
        {
            _firstName = "Jane";
        }
        return _firstName;
    }
}
string _firstName;
Or even
public string FirstName 
{
    get 
    {
        return _firstName ?? (_firstName = "Jane");
    }
}
string _firstName;
With auto properties we to be able to write is as something like:
public class Person
{
     public string FirstName { get; set; } ?= "Jane";
} 
Or
public class Person
{
     public string FirstName { get; set; } ??= "Jane";
} 
Regards
Sep 14, 2014 at 7:01 PM
I prefer my approach of Overridable Default Values For Null?
class Person( string! FirstName , string! surname )
{
  public string! FirstName { get: private set: }
  public string! Surname   { get: private set: }
  overrides Person! default() { return New Person( "Jane", "Doe" ); }  
}  
Sep 14, 2014 at 7:29 PM
I see, good idea as well.
I cannot see a conflict between both.
Yours is fine for "primary" properties, which could be initialized by default primary constructor.
My suggestion can be used on any/many properties and it is going further than just be able to initialize a property,
but returning some default value even when is NULL or set to NULL.
Regards
Sep 14, 2014 at 7:45 PM
Given that resources are limited, which feature being implemented would you take out to put this one in.
Sep 14, 2014 at 8:23 PM
velio wrote:
Yours is fine for "primary" properties, which could be initialized by default primary constructor
Why do people assume that my method involves a default constructor. It doesn't need a default constructor, the constructor(s) can be private.
class Person
{
 public readonly string! FirstName() { get: }
 public readonly string! Surname() { get: }
 private Person( string! Firstname, string! Surname )  {  this.FirstName = FirstName ;  this.Surname = Surname ;  }
 public static Person! CreateNew ( String! First , String! Surname ) { return new Person( First, Surname );  }
 overrides Person! default() { return Person.CreateNew( "Jane", "Doe"); }
}
Sep 15, 2014 at 6:57 AM
Paulo, I would be glad at least to try put that feature in.
It would be a very interesting journey for me.
I'm going to get the code and start working on it these days.

Adam, right, I think I get it now.
You are suggesting we been able to override what default(T) is returning and that been forced by T! as well.
Still, I cannot find a conflict between both ideas.

Regards
Sep 15, 2014 at 1:26 PM
I wish the CLS had defined an attribute which would compel particular instance members to be invoked with call rather than callvirt. Had they done that, it would have been easy to have types which must be stored on the heap but should have value semantics (e.g. strings) to have a default value that would behave like a non-null value. It should be possible to say if (someString.IsNullOrEmpty) using a property defined within the String class, rather than having the only way to accomplish such a thing being the use of an extension method which must be defined elsewhere.