1
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
    They are the same. The second was introduced in a later version of C# – Dennis_E Oct 01 '19 at 22:26
  • They are identical in compiled code and intent. The second was introduced in order to make it possible to use expression-bodied syntax also for the setter. – Lasse V. Karlsen Oct 01 '19 at 22:27
  • _"Starting with C# 7.0, both the get and the set accessor can be implemented as expression-bodied members"_ - [docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties#expression-body-definitions) – stuartd Oct 01 '19 at 22:29
  • 1
    In Visual Studio, if you hover the mouse over both identifiers you'll see it shows the same snippet for both: `DateTime Date { get; }` – Lance U. Matthews Oct 01 '19 at 22:30
  • This is surely a duplicate – TheGeneral Oct 01 '19 at 23:38
  • @TheGeneral I said the same thing to myself, but couldn't find any questions specifically comparing the two expression-bodied property syntaxes. [What is the difference between getter-only auto properties and expression body properties?](https://stackoverflow.com/q/27910985/150605) and [Difference between (auto) properties initialization syntax in C# 6](https://stackoverflow.com/q/33235561/150605) are the closest I could find. – Lance U. Matthews Oct 02 '19 at 19:34

2 Answers2

3

Both ways result in the same IL.

For

using System;
public class Program {
    public DateTime MyDate1 { get => DateTime.UtcNow; }
    public DateTime MyDate2 => DateTime.UtcNow;
    public void Main() {
    }
}

the abbreviated IL for both versions is (I used https://sharplab.io):

    // Methods
    .method public hidebysig specialname 
        instance valuetype [mscorlib]System.DateTime get_MyDate1 () cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 6 (0x6)
        .maxstack 8

        IL_0000: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_UtcNow()
        IL_0005: ret
    } // end of method Program::get_MyDate1

    .method public hidebysig specialname 
        instance valuetype [mscorlib]System.DateTime get_MyDate2 () cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 6 (0x6)
        .maxstack 8

        IL_0000: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_UtcNow()
        IL_0005: ret
    } // end of method Program::get_MyDate2

(...)

// Properties
    .property instance valuetype [mscorlib]System.DateTime MyDate1()
    {
        .get instance valuetype [mscorlib]System.DateTime Program::get_MyDate1()
    }
    .property instance valuetype [mscorlib]System.DateTime MyDate2()
    {
        .get instance valuetype [mscorlib]System.DateTime Program::get_MyDate2()
    }

tymtam
  • 31,798
  • 8
  • 86
  • 126
1

For all intents and purposes they are the same. They are compiled the same. You can use whichever you prefer.

kkica
  • 4,034
  • 1
  • 20
  • 40