Questions tagged [expression-bodied-members]

9 questions
2
votes
1 answer

Does Swift support Expression-bodied members?

In C#, there's a feature called 'Expression-bodied members' which lets you use an expression to satisfy the body of a function/method or property. For instance, instead of writing these... void PushItem(object item) { …
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
1
vote
2 answers

Is there a difference between the following ways of using expression-bodied syntax in c#?

public DateTime Date => DateTime.Now; public DateTime Date { get => DateTime.Now; } Is there a difference between the two or is it equivalent code with different syntax?
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
1
vote
0 answers

Expression bodied members + WCF =?

If I have a WCF service project where one of the classes used in the service looks like this: public class Test { public int Num { get; set; } public bool IsHigh => Num > 7; } Then the expression bodied member seems to go missing in the…
Mystogan
  • 547
  • 4
  • 21
0
votes
0 answers

Using IDisposable in expression-bodied member

ShellFile implements IDisposable. public string LocalizedName => new ShellFile(Executable).FileDescription; I was unable to find anything on the internet about using disposable objects in expression-bodied members. Is the ShellFile instance…
0
votes
1 answer

Expression bodied member equivalent

I'm just learning expression bodied members for class properties, and the basics are easy enough, but I'm trying to determine if the following getter could even be written as an expression bodied member - not for performance gain or anything, simply…
Jesse Williams
  • 653
  • 7
  • 21
0
votes
0 answers

Expression Bodied fields (read-only) in structs does not get copied using Reflection

I am trying to implement object deep/shallow cloning service using Reflection. Using the function Clone Simple class is being copied with all the required fields, but in case of SimpleStruct Computed field does not get copied. What is the…
-1
votes
2 answers

What is the equivalent for expression-bodied members in old C# versions?

In new versions of C# I have discovered recently that exist what is called expression-bodied-members as explained here. An example of expression-bodied-members would be (for a getter property only): private int _x; public int X { get =>…
Willy
  • 9,848
  • 22
  • 141
  • 284
-1
votes
1 answer

Why use expression-bodied properties for primitive values?

What are the pros and cons of expression-bodied properties vs straight property declarations? For example, is there any advantage to using; public string Foo => "Bar" vs simply public string Foo = "Bar" My understanding was the => is used when the…
TomDane
  • 1,010
  • 10
  • 25
-2
votes
1 answer

Is it possible to write an expression-bodied member containing an if statement?

I wonder if it is possible to rewrite the following method using an expression-bodied member: private void Checkup() { if (errorCondition) throw new InvalidOperationException("Error"); }
G. Lari
  • 435
  • 2
  • 14