This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 5 years ago.
The code below is used to initialise a store in some C# code I am looking at. However I do not understand the => syntax in this context.
Any ideas?
protected new ServiceRepositoryStore<T> Store => (ServiceRepositoryStore<T>) base.Store;
That is an example of an expression bodied read-only property:
https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
It's a C# 6.0 feature and is the equivalent of:
protected new ServiceRepositoryStore<T> Store
{
get
{
return (ServiceRepositoryStore<T>) base.Store;
}
}
Related
This question already has answers here:
what is this feature called in c# and which version of c# uses this
(3 answers)
Closed 3 years ago.
Does "PictureBox[] pictureb => "code mean using pictureb as a get method with picturebox array type. Controls.OfType<PictureBox>().ToArray(); means control properties using oftype which is ienumerable method convert to array.
Let's break down your code
public PictureBox[] pictureb => Controls.OfType<PictureBox>().ToArray();
into smaller manageable pieces.
If you were to declare a read-only property in - say C# 3.0 - you would use syntax similar to this.
public PictureBox[] picture
{
get
{
/* Some code which returns a PictureBox array. */
}
}
C# 6.0 - I believe added expression bodied members. So we can abbreviate the above code somewhat with this syntax.
public PictureBox[] pictureb => Controls.OfType<PictureBox>().ToArray();
The portion after => is the body of this property, which is Controls.OfType<PictureBox>().ToArray(); in this case.
This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
I came across this in our code-base today, and it took me awhile to see what the effect of it was, but what in the world does this actually mean??
public virtual SomeClass InstanceVariable => new SomeClass("arg1", "arg2");
I played around with this in Visual Studio's C# interactive terminal and discovered that it appears to be equivalent to:
public virtual SomeClass InstanceVariable { get { new SomeClass("arg1", "arg2"); } }
However, I couldn't find any documentation on this being any form of 'syntactic sugar' for a read-only property.
Someone want to shed some light on the scenario?
It is from new C# 6.0. You can instantiate your class by default. Check "Expression Bodied Functions and Properties" of https://msdn.microsoft.com/en-us/magazine/dn802602.aspx article
This question already has answers here:
Call constructor as a function in C#
(3 answers)
Closed 7 years ago.
I have the following lambda expression:
Something((o, i) => new TheNewSwiss(o, i));
Is there a shorthand syntax? Something simliar to this but for ctors?
Something((o, i) => TheNewSwiss.New(o,i));
Something(TheNewSwiss.New);
No, there's no equivalent of method group conversions for constructors. (I can see it being handy, but it doesn't exist.) You just need to use the lambda expression syntax you've got in your first snippet.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.
This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 8 years ago.
Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned?
ie:
public class TheClass
{
private var aList = new List<string>();
}
Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done?
Here's a blog post from Eric that explains the reasoning.