Questions tagged [expression-body]

This tag can represent question about Expression Body [=>] operator for C#

This tag can represent question about Expression Body operator for C#.

16 questions
5
votes
2 answers

What does the first arrow operator in this Func mean?

Given this example code: enum op { add, remove } Func combo(string head, double tail) => (op op) => op == op.add ? Int32.Parse(head) + Convert.ToInt32(tail) : Int32.Parse(head) -…
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
5
votes
2 answers

Expression-bodied members vs Lambda expressions

A lambda expression is a block of code (an expression or a statement block) that is treated as an object. It can be passed as an argument to methods, and it can also be returned by method calls. (input parameters) => expression SomeFunction(x =>…
Llazar
  • 3,167
  • 3
  • 17
  • 24
4
votes
2 answers

Changing a property to expression-bodied based on ReSharper leads to error?

one of my properties looks like this: public string Name { get{ return _name; } set { _name = value; } } but ReSharper is advising me to change it to: public string Name { get => _name; set => _name = value; } if I refactor like that then…
nshathish
  • 417
  • 8
  • 23
2
votes
1 answer

C#: How to declare a checked switch-expression-bodied function?

I'm stuyding C# (10.0/.NET6) and I've got a question. There is a code: Int32 x = Int32.MaxValue; Int32 y = Int32.MaxValue; try { WriteLine($"{x} * {y} = {SomeFuncSwitch(x, y, 2)}"); WriteLine($"{x} + {y} = {SomeFunc(x, y)}"); } catch (…
1
vote
0 answers

Are expression-bodied methods automatically inlined?

Are expression-bodied methods automatically inlined? Are the following equivalent in non-debug compilation? public byte[] GetBytes(char value) => GetBytesInternal(value, 2); [MethodImpl(MethodImplOptions.AggressiveInlining)] …
Herman Schoenfeld
  • 8,464
  • 4
  • 38
  • 49
1
vote
1 answer

Converting block body method with out parameter to expression body method causes out parameter to be null

I have the following method which is covered by passing unit tests; public static bool TryGetInstance(out T config) where T : class { return Instance.TryGetInstance(out config); } When I convert this to expression body syntax…
mark_h
  • 5,233
  • 4
  • 36
  • 52
1
vote
2 answers

Multiple values in an expression-bodied set method of a property

Is it possible to set multiple values in a set method? I want to do something like the following: public int ID { get; set => {Property = value, ID=value}; }
Wouter
  • 1,293
  • 2
  • 16
  • 34
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
2 answers

Expression-bodied members doesn't create new instances?

I'm curiuous why Expression-bodied properties doesn't create persisant objects. public List Pages { get; } = new List(); Does create a persistant isntance of List, just like always. But public List Pages => new…
SpReeD
  • 7
  • 5
0
votes
2 answers

Declare inline variables everywhere in C# (like in a base-constructor-call where declaring is not feasible)

There are places where you can not declare new variables like in a base-constructor call (Exclaimer: this is an example to show-case the problem): public class SportLicense : BaseEntity { public SportLicense() : base( …
Daniel
  • 486
  • 4
  • 19
0
votes
0 answers

Expression body constructor in ViewModel giives build errors

For some reason, I'm getting build errors when trying to use an expression body for the constructor if a viewmodel. This works: public class SolutionMaintenanceViewModel { public SolutionMaintenanceViewModel(IActiveUserService currUserSrv) …
Sarov
  • 545
  • 6
  • 17
-1
votes
3 answers

Expression bodied Function Weird Behavior

When I am using var frontPage = await GetFrontPage(); protected override async Task GetFrontPage() { return null; } This code works fine and I am getting null value in frontpage variable. but when I am…
-1
votes
1 answer

instantiate object property on get in one line

This is a question out of curious more so than actual need, but I thought it would be interesting to find out, since I have classes soo deep I need 10 lines of instantiation just to set a single property. The property [MyObj3] functionally works as…
-1
votes
1 answer

why is the return keyword not required in expression bodied methods

Looking at expression bodied methods, I am trying to understand why the return keyword is not used. For example with public int GetAge() { return this.Age; } I would have expected public int GetAge() => return this.Age; as opposed to the…
Tim
  • 115
  • 7
-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
1
2