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
Related
This question already has answers here:
How to find out which interfaces a .net class implements?
(4 answers)
Closed 4 years ago.
Is there a way to get a list of all interfaces of a C# class in the Visual Studio UI without digging through the superclass chain step by step? If not even a list on MSDN would be useful.
For example I can't see that Form is IDisposable without digging down to Control.
GetInterfaces:
typeof(List<string>).GetInterfaces()
returns
Type[] (8 items)4
typeof(IList<String>)
typeof(ICollection<String>)
typeof(IEnumerable<String>)
typeof(IEnumerable)
typeof(IList)
typeof(ICollection)
typeof(IReadOnlyList<String>)
typeof(IReadOnlyCollection<String>)
If you need to know a particular interface, you can use IsAssignableFrom:
typeof(ICollection).IsAssignableFrom(typeof(List<string>))
returns true
This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
While researching C# operator overloading, I stumbled across this block of code on the MSDN web site:
public static Complex operator +(Complex c1, Complex c2) =>
new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
// Override ToString() to display a complex number
// in the traditional format:
public override string ToString() => $"{this.real} + {this.imaginary}";
This is a really useful way of defining simple methods in certain circumstances, but I don't recall ever seeing it described anywhere. I tried searching the C# 5.0 language specification for a description of this method declaration syntax, but could find nothing. I also found nothing in my web searches.
Two questions:
In which version of C# did this method declaration syntax become available?
Where in the language specification is this syntax described?
It was added in C# 6, you can read about it on the official Github of the new compiler here.
This question already has answers here:
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Objects implicitly instantiated in vb.net?
(2 answers)
Closed 7 years ago.
Recently I've picked an old project in VB.NET and I keep finding things like this that annoy me:
Dim answer as String
answer = Form_Input_Text.Lbl_Answer.Text
Apparently, in VB.NET it is allowed to access stuff which should belong to an instance of a Form as if it is something static. In C# one would need to write this:
string answer = null;
using (Form_Input_Text my_form = new Form_Input_Text())
{
//stuff
answer = my_form.Lbl_Answer.Text;
}
The first bit of code is compiled with no errors, but sometimes I get NullReferenceException as expected.
How do I enforce this behaviour on VB.NET?
This question already has answers here:
Why doesn't the C# compiler stop properties from referring to themselves?
(6 answers)
Closed 9 years ago.
Accidentally I created a bug in my work. The cause of the bug is a snippet of c# code like
public ClassA{
public string AProperty
{
get
{
return AProperty;
}
set
{
AProperty = value;
}
}
}
You can build that, and running that without any exceptions.
But it will create an infinite loop. For example, while setting the AProperty, it always tries to assign the value to its self.
Anyone knows why in C# the compiler will allow this syntax?
Because the C# compiler team didn't decide to make this exact case illegal. Possibly because they didn't consider the benefits of implementing it to be greater than the costs.
It's something that comes up with even the most trivial amount of testing; it's not a very subtle bug, and it's something that developers learn to look for if they get bitten by it a few times.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
CompilerServices.Operators equivalent on C#
I was looking for Microsoft.CSharp.CompilerServices.Operators but couldn't find it.
No there is no real equivalent to this in the C# runtime assembly.
However many these methods are essentially implementing the late bound operations for VB.Net in a declarative method (and indeed there are cases where the late binder simply just defers to these methods for operations). So these could be replicated in C# by defining methods which just explicitly defer to the C# dynamic binder.
For example, the rough equivalent of DivideObject in C# would be the following
public static dynamic DivideObject(dynamic left, dynamic right)
{
return left / right;
}