New keyword { get; set; init; } #2747
Replies: 22 comments
-
Why not simply use |
Beta Was this translation helpful? Give feedback.
-
With target typed new, this becomes:
|
Beta Was this translation helpful? Give feedback.
-
How is this superior to this:
Target Typed new has already been proposed. |
Beta Was this translation helpful? Give feedback.
-
Superior in terms of beauty and functionality eg: public class Abc
{
public List<string> Property {
get;
set;
init {
value.add("Apple");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
We do not think of a code with many properties, as we can see below. Ugly example: public class Abc
{
public List<string> Property {get; set;} = new List<string>();
public List<string> Property2 {get; set;} = new List<string>();
public List<string> Property3 {get; set;} = new List<string>();
public List<string> Property4 {get; set;} = new List<string>();
public List<string> Property5 {get; set;} = new List<string>();
public List<string> Property6 {get; set;} = new List<string>();
public List<string> Property7 {get; set;} = new List<string>();
public List<string> Property8 {get; set;} = new List<string>();
public List<string> Property9 {get; set;} = new List<string>();
public List<string> Property10 {get; set;} = new List<string>();
public List<string> Property11 {get; set;} = new List<string>();
public List<string> Property12 {get; set;} = new List<string>();
public List<string> Property13 {get; set;} = new List<string>();
public List<string> Property14 {get; set;} = new List<string>();
} Beautiful example: public class Abc
{
public List<string> Property {get; set; init; }
public List<string> Property2 {get; set; init; }
public List<string> Property3 {get; set; init; }
public List<string> Property4 {get; set; init; }
public List<string> Property5 {get; set; init; }
public List<string> Property6 {get; set; init; }
public List<string> Property7 {get; set; init; }
public List<string> Property8 {get; set; init; }
public List<string> Property9 {get; set; init; }
public List<string> Property10 {get; set; init; }
public List<string> Property11 {get; set; init; }
public List<string> Property12 {get; set; init; }
public List<string> Property13 {get; set; init; }
public List<string> Property14 {get; set; init; }
} |
Beta Was this translation helpful? Give feedback.
-
Why do you expect an |
Beta Was this translation helpful? Give feedback.
-
I think the infinite would be an embellishment of language. In the Example public class Abc
{
public List<string> Property {get; set; }
}
public class Cba
{
public Cba()
{
var abc = new Abc();
abc.Property.Add("Apple");
}
} Solution Ugly public class Abc
{
public List<string> Property {get; set; } = new List<string>();
}
public class Cba
{
public Cba()
{
var abc = new Abc();
abc.Property.Add("Apple");
}
} Example beautiful public class Abc
{
public List<string> Property {get; set; init; }
}
public class Cba
{
public Cba()
{
var abc = new Abc();
abc.Property.Add("Apple");
}
} |
Beta Was this translation helpful? Give feedback.
-
That doesn't answer any of my questions/concerns. Why should this |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
Reply: The init keyword is also superior because it is only instantiated when called |
Beta Was this translation helpful? Give feedback.
-
What are the mechanics by which that woulf work? |
Beta Was this translation helpful? Give feedback.
-
So then this is a duplicate of the various "lazy" property proposals already on this repo. And it also falls to the same problem as those proposals, namely that threading and publishing policy now become concerns. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The question I want to raise here is today property calls are very brown in time Keyword
Requirements:
Success public class Abc
{
public List<string> Property { get; set; init; }
public Abc Property2 { get; set; init; }
} Fail public class Abc
{
//Fail
public decimal Property { get; set; init; }
//Fail
public string Property2 { get; set; init; }
// Fail
public double Property3 { get; set; init; }
} Save the memory In this example I will show when the keyword public class Abc
{
public List<string> Property { get; set; init; }
public Abc Property2 { get; set; init; }
}
public class Cba
{
public Cba()
{
var cba = new Abc();
// The init keyword kicks in this part, it checks if the property is null
// if null it will automatically do the new List <string> ()
cba.Property.Add("Apple");
}
}
|
Beta Was this translation helpful? Give feedback.
-
I don't find that code at all easier to understand, especially given it doesn't remotely describe what it's going to do, or when, or what happens in the myriad of edge cases that will arise, such as when multiple threads attempt to access the property, or if you explicitly attempt to assign |
Beta Was this translation helpful? Give feedback.
-
Save the memoryIn this example I will show when the keyword init comes into action. public class Abc
{
public List<string> Property { get; set; init; }
public Abc Property2 { get; set; init; }
}
public class Cba
{
public Cba()
{
var cba = new Abc();
// The init keyword kicks in this part, it checks if the property is null
// if null it will automatically do the new List <string> ()
cba.Property.Add("Apple");
}
} |
Beta Was this translation helpful? Give feedback.
-
So, that means that every single access of the property has to do a |
Beta Was this translation helpful? Give feedback.
-
This isn't an idiom I've ever heard before - can you please explain? |
Beta Was this translation helpful? Give feedback.
-
So, that means that every single access of the property has to do a null check? And it's impossible to set the property to null explicitly? And that read-only properties cannot be init? Example success: public class Abc
{
private readonly List<string> _property { get; set; init; }
public readonly List<string> Property1 { get; set; init; }
public void Add()
{
_property.Add("Apple");
Property1.Add("Dog");
}
} And what if two threads attempt to access the property concurrently that they will end up overwriting each other's instance? |
Beta Was this translation helpful? Give feedback.
-
Beauty clearly is in the eye of the beholder here. Your code example, public class Abc
{
public List<string> Property {
get;
set;
init {
value.add("Apple");
}
}
} is certainly not beautiful in my view. You aren't following the common C# brace style convention, and the code is long and rambling, so is far from beautiful. And as others have told you, target typed-new is proposed for a future language version, and so you code example possibly could then be expressed as: public class Abc
{
public List<string> Property { get; set; } = new { "Apple" };
} which is far superior in my view. Others may disagree though as the subject of code beauty is so subjective. I therefore see no benefit to this proposal. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Tiny correction: based on the proposal, I believe the syntax would have to be: public List<string> Property { get; set; } = new() { "Apple" }; Though that doesn't change anything about what you said. |
Beta Was this translation helpful? Give feedback.
-
You say beauty, I say clarity. With |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This proposal comes to make the code cleaner since
get
andset
came to help us programmers. I believeinit
will make the code less polluted when we need to initialize many properties, avoiding the famous NullPointerException.I think the good use of the technique will make the code look better.
Today:
Solution:
Beta Was this translation helpful? Give feedback.
All reactions