Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin sample projects not compiling

I just started to look at Xamarin and downloaded a prebuilt contacts sample app.

It does not compile in Visual Studio because it is full of strange syntax I never saw before, searching Google didnt fruit..

For eample, this is weird:

[JsonIgnore]
public string AddressString => string.Format(
    "{0} {1} {2} {3}", Street,
    !string.IsNullOrWhiteSpace(City) ? City + "," : "",
    State, PostalCode);

[JsonIgnore]
public string DisplayName => ToString();

[JsonIgnore]
public string DisplayLastNameFirst => $"{LastName}, {FirstName}";

[JsonIgnore]
public string StatePostal => State + " " + PostalCode;

public override string ToString()
{
    return FirstName + " " + LastName;
}

These strange lambda expressions - what are they? Why are they not "="? And the $ signs? The .NET compiler spits them all out.

This is another one:

static int MatchScore(Acquaintance c, string query)
{
    return new[]
    {
        $"{c.FirstName} {c.LastName}",
        c.Email,
        c.Company,
    }.Sum(label => MatchScore(label, query));
}

The compiler says { and } are expected...

What did I miss in the last 5 years???

like image 372
Paceman Avatar asked Jul 25 '26 18:07

Paceman


1 Answers

The strange syntax that you see is C# 6 syntax. You will need to open the project in VS 2015 to compile it.

The first one with lambda expressions is Expression Bodied Members. You can rewrite the first example as

 public string AddressString 
 { 
    get 
    { 
        return string.Format("{0} {1} {2} {3}",
              Street, !string.IsNullOrWhiteSpace(City) ? City + "," : "",
              State, PostalCode);
    }
}

The second one with $ sign is string interpolation and you can replace it with string.Format

like image 166
Giorgi Avatar answered Jul 28 '26 08:07

Giorgi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!