Syntactic sugar?

Asked

Viewed 615 times

2

I came across the term syntactic sugar these days when I was studying the use of await and async in C#. This question explains what syntactic sugar or syntax sugar is What is syntax sugar and how it works?.

Besides await and async, which others exist in C#?

  • 3

    I think this question is too wide. There is no such clear boundary on where "sugar" ends and where "language" begins. For example, the operator += is also sugar. The using is a sweetened version of a code where certain features are released manually. There will be dozens of examples.

  • I was just going to ask using if it was a sugar syntactic.

  • @bfavaretto Having dozens of examples does not make the question too broad. Of course the definition of syntatic sugar is not exact, but even if you consider even example as += (that I don’t think are sugary) the list is finite and well defined.

1 answer

1

It is difficult to separate exactly what is exactly sugary from what is not. It is also difficult to define all the sweets of C#, but here are some that make the code much clearer.

lock

That:

lock (_lock)
{

}

In place of:

object obj;
var temp = obj;

Monitor.Enter(temp);

try
{
    // body
}
finally
{
    Monitor.Exit(temp);
}

foreach

That:

IEnumerable<int> numeros = new int[]
{
    1, 2, 3, 4, 5
};

foreach (int i in numeros)
{

}

In place of:

var enumerator = numeros.GetEnumerator();
while (enumerator.MoveNext())
{
    int current = enumerator.Current;
}

using

That:

using (FileStream f = new FileStream(@"C:\foo\teste.txt", FileMode.Create))
{

}

In place of:

FileStream f2= new FileStream(@"C:\foo\teste.txt", FileMode.Create);
try
{

}
finally
{
    f2.Dispose();
}

Auto properties

That:

 public string Foo { get; set; }

In place of:

private string _foo;
public string Foo
{
    get { return _foo; }
    set { _foo = value; }
}

Nullable

That:

int? nullInt2 = null;

In place of:

Nullable<int> nullInt = new Nullable<int>();

Operator `??`

That:

int myNum = nullInt ?? 0;

In place of:

int myNum2 = nullInt == null ? 0 : nullInt.Value;

I changed the response to community wiki to extend and improve

  • I have removed my answer. I do not understand why you close the question.

  • I agree it’s broad but not broad too.

  • 1

    @ramaral This comment explains the breadth of the question. Anything can be considered syntactic sugar, even redefinitions that the programmer himself create. This same answer says that it is difficult to separate what is from what is not. Thus, the question could continue to receive dozens of answers and all would be, in principle, correct (which does not mean that they would be useful to any reader).

  • 1

    @Luizvieira I agree with everything that was said in the comment, that is why I created my answer as wiki. However I think they were too strict, not least because the AP asks for examples which does not require naming all cases.

  • 2

    @ramaral I understand your point. A single wiki response would certainly be better than a flood of opinionated responses. But it has to be considered that SOPT is not a forum, but a Q&A site that aims to have concrete and well defined questions and answers. So I don’t think it was too strict.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.