This project is read-only.
1
Vote

"using" or "with" keyword to set properties

description

I have a suggestion for a new c# feature to set properties (Like Visual Basic "With" keyword).
var person = new Person();

using (person)
{
    Name = "Bill",
    LastName = "Gates"
}

comments

JoshVarty wrote Oct 3, 2014 at 9:59 PM

You can already accomplish this in C#:
var person = new Person()
{
    Name = "Bill",
    LastName = "Gates"
};

wilmerferreira wrote Oct 3, 2014 at 11:22 PM

I'm not talking about object initializers (which were introduced in C # 3.0), the idea is NOT to construct a new object, the idea is to USE a current object to assign values ​​to the properties of an easier way.

Example
namespace Demo
{
    public class Person
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }

    static class Program
    {
        static Person GetPersonById(int id)
        {
            var p = new Person();
            p.Id = id;

            return p;
        }
        static void SavePerson(Person person)
        {
            //TODO: Save person
        }


        static void Main()
        {
            var person = GetPersonById(1);

            // Begin suggestion

            using (person) // or could be another keyword, maybe 'with'
            {
                FirstName = "Bill",
                LastName = "Gates"
            }

            // End suggestion

            SavePerson(person)
        }
    } 
}
The idea is to reduce the code we write, even leave it more elegant.

In Visual Basic already exists through the WITH keyword (http://msdn.microsoft.com/en-us/library/wc500chb.aspx).

Vlad5Maxed wrote Oct 7, 2014 at 5:03 PM

No no and no. Trust me it is very very bad idea. I had enough problems in Pascal and Delphi with With statement.

There are short list of potential problems:
  1. Debugging. When you move mouse over FirstName debugger should understand that it is part of using and evaluate properly. Without this it is much harder to debug application.
  2. Nesting. It is just became nightmare when developers start nesting them. And #1 still apply to it.
  3. Scope. Assume that program has property called FirstName. Then compiler should check this and not let you use FirstName in using and force you to specify full name. Without compiler support it just way to get strange bugs after say you remove FirstName from person and it will still works just not how you expected.
  4. Refactoring. It should support this as well
  5. Understanding. If developer start using long using statements (or nest them) it is very hard to understand code. Even in your case it is totally not obvious that FirstName and LastName belongs to person.
  6. Edit and Continue should support that feature as well.
As well actual typing is so tiny fraction of product development that time saved on typing
person.FirstName = "Bill"'
person.LastName = "Gates"
is totally not worth spending time of Roslyn developers.

In our Delphi team we totally prohibit of using With statement.

Vlad