2

While checking some repo I found out this and it broke my mind.

Example:

 var config = Config.GetConfig();
 var gen = new MatDocumenationGenerator(typeof(BaseMatDomComponent).Assembly,
            Path.Combine(config.Path, "MatBlazor.Demo", "Doc"));
 {
 }
 ;
 gen.Generate();

How it's possible to have { }; inside a method? Why it isn't a compile error?

I'm not sure but maybe it's some feature of the language? If yes, what does this do or is used for? How it works?

I tried to search what is this, but I don't even know how to call this, so, is there a name for this?

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • Does this answer your question? [C# - meaning of curly braces after the "is" operator](https://stackoverflow.com/questions/62139886/c-sharp-meaning-of-curly-braces-after-the-is-operator) or [this](https://stackoverflow.com/questions/62212715/what-does-is-syntax-mean-in-c), or [this](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/patterns) – Carlo Zanocco Aug 24 '20 at 12:55
  • 7
    By itself, `{ };` is an empty block followed by an empty statement. So it has no effect. – Aluan Haddad Aug 24 '20 at 12:58
  • 4
    Other commenters, the example has nothing to do with pattern matching nor object initialization. – Joe Sewell Aug 24 '20 at 12:58
  • 1
    @mason - no its not... there is a semicolon on the line before it, therefore it is not linked to the new statement, and just defines an empty block. Maybe it was intended as that and the semi colon was a mistake, but its defos not working like that – Milney Aug 24 '20 at 12:59
  • 1
    I would agree that it is simply an empty block followed by an empty statement, but that explicitly setting every part on a new line makes me somewhat suspicious. Maybe worth to do a blame and ask the original author? – Fildor Aug 24 '20 at 13:02
  • Can anyone reference some doc about this? Because now I'm confused. Gold badge in C# guy say it's object initializer, others say it's scope... – Vencovsky Aug 24 '20 at 13:05
  • @Vencovsky Most of us are confused. We can figure out, what this is and that it does nothing, really. Probably the optimizer will throw it out completely. What's confusing is why it _is_ there in the first place... – Fildor Aug 24 '20 at 13:07
  • 1
    It could just be a typo. That the intention was to use an object initializer, but added a `;` by reflex. – Martin Backasch Aug 24 '20 at 13:08
  • Voting to reopen because the duplicate question does not have the `;` following the block, nor does it explain how empty blocks can appear. – Joe Sewell Aug 24 '20 at 13:17
  • I have the suspicion this has been introduced accidentally by merging in a pull request ... – Fildor Aug 24 '20 at 13:18
  • 1
    @Vencovsky I originally said it was an object initializer, I missed the semicolon at the end of the line. Sorry for any confusion I might have caused. It's just a nested code block that does nothing in this case. I think it was *meant* to be an object initializer by the writer of the code, since it always follows a constructor, but in actual effect it's just an empty code block. See the duplicate that Tom marked this as. – mason Aug 24 '20 at 13:18
  • @JoeSewell Since that semicolon makes it part of a different line, it's irrelevant. The duplicate is perfectly suitable. – mason Aug 24 '20 at 13:19
  • 2
    a line of a thousand semicolons does not generate a warning either. related: https://stackoverflow.com/a/2374612/1132334 – Cee McSharpface Aug 24 '20 at 13:33
  • 2
    according to git blame, it was [added in one go](https://github.com/SamProf/MatBlazor/commit/1ec107fd0cd044485b4d69876f605711d191a6ca#diff-ed7c9da2ff9c3e4feecbe249679d36f1R26), does not look like a merge artifact. it is likely a harmless mistake. if it was done on purpose, only [the author](https://www.samprof.com/) can answer this. – Cee McSharpface Aug 24 '20 at 13:42

2 Answers2

4

It's valid code that does nothing, and is redundant. It's almost certainly a mistake and could be removed without affecting the program. The compiler doesn't warn about everything that's redundant, although I agree it possibly could and should in this case.

Rich N
  • 8,939
  • 3
  • 26
  • 33
  • It's easy to understand that the code does nothing, but how does it work? Could you explain that please? And by "how does it work" I mean, it is really going on with `{ };`. Why it's possible? – Vencovsky Aug 24 '20 at 13:09
  • 1
    "{}" is a legal statement and ";" is another legal statement. Both do nothing and will probably just be optimized away and not even appear in IL Code (how would they? As NOPs at best). – Fildor Aug 24 '20 at 13:11
  • @Vencovsky because C# permits a block (`{...}`) in that context and empty blocks are permitted. The `;` does not associate with the block – Aluan Haddad Aug 24 '20 at 13:13
  • @Fildor saying `"{}" is a legal statement and ";" is another legal statement` is like saying `X works because it works, Y works because it works`. I understand what you say about that, but still, no explanation – Vencovsky Aug 24 '20 at 13:14
  • I think the true explanation is hidden in the "duplicate" post. `{}` works because it's a "inner" scope. And you can open as many scopes you want. The same with `;` you can have as many `;` you want, making some crazy think like `{}{}{}{;;;;}{};;;;{}` a valid c# code – Vencovsky Aug 24 '20 at 13:16
  • Yes, I suppose the 'reason' is ; works on its own because we can have an empty line that does nothing, and { } works on its own because we can have an empty scope block that does nothing. There's no point in these things though, so the compiler could warn in both cases. – Rich N Aug 24 '20 at 13:19
  • @Fildor I just explained, please read my comment above. Rich N also did it – Vencovsky Aug 24 '20 at 13:20
  • @Vencovsky Saying "X works because it works" is often how you'll use to describe code compiling. We see questions like yours all the time, saying "why does this work" and the answer is "The people writing the compiler didn't take the time to make it not work, because it wasn't necessary to do so". In other words, it works because it works. – mason Aug 24 '20 at 13:22
  • 1
    @Vencovsky Yep, just did. I think that's it, basically. There's really nothing more to it. And as I commented on the question, my suspicion is that it was introduced by accident while merging in a pull request. – Fildor Aug 24 '20 at 13:22
  • ^^ That being said, the authors may appreciate a hint, so they can clean it up. – Fildor Aug 24 '20 at 13:26
  • ok I can prepare a PR but what do we tell him? your intellisense hiccup from a decade ago has enthralled distinguished random strangers on the internet, pondering a secret meaning behind an accidential NOP? :) – Cee McSharpface Aug 26 '20 at 23:16
1

You can use {...} in almost every language. Java, C++, C#... It simply opens a Scope which defines the lifetime of variables that live on the Stack (not on the heap)

Example:

void someFunction () { 
    // some code here
    {
        var a = 5;
    }

    // 'a' does not exist anymore after the '}'

}
KYL3R
  • 3,877
  • 1
  • 12
  • 26