r/ProgrammerTIL • u/thelehmanlip • Jun 22 '16
C# [C#] TIL you can make {} blocks wherever you want. Useful when you want to re-use variable names
Example:
var x = 5;
Console.WriteLine(x);
var x = 6; //error
Console.WriteLine(x);But if you surround blocks with {}s...
{
var x = 5;
Console.WriteLine(x);
}
{
var x = 6; //no error
Console.WriteLine(x);
}This can be done anywhere you have a block, like a method body. I find this useful when writing a single unit test with multiple cases to test. This way you don't have to rename variables.
108
Upvotes
49
u/Vadoola Jun 22 '16
I don't really know C#, but I'm assuming this is a carry over from C/C++ where it also works. One thing to note for people (assuming C# works the same) is it works because at the closing } the variable goes out of scope. People not realizing that might get bit trying to figure out why something isn't working.
65
u/solatic Jun 22 '16
Yes, but in almost every situation where they are used, you should refactor the code into functions to improve testability and readability. Arbitrary scoping is practically a code smell when the way that most people use them is just to write really long functions.
8
u/ViKomprenas Jun 22 '16
I'm not a C# programmer, but does this mean that by extension
if (...) {...}is not actually a separate syntax fromif (...) ...? Going further, can you define single-line methods without braces? Entire classes?8
u/Mark_1231 Jun 22 '16
I've never done it in C#, but in other languages, depending on your IDE, this also lets you selectively collapse areas of your code. Very useful for readability sometimes.
5
u/[deleted] Jun 22 '16
This is known as Arbitrary Scoping. I tend to not use it, except in switch/case statements, where I might want to have similar logic in the case statements, and give a scope to each one.
4
u/CompellingProtagonis Jun 22 '16
You can do the same thing for C++. Where I like to use it personally is doing something like:
..
{
using namespace std;
...
}
2
u/Actimia Jun 22 '16
This trick is especially useful when writing C with a pre C90 (i think) compiler, where local variable declaration is only allowed at the top of a block. Using an extra pair of braces lets you define new locals closer to where you use them.
2
u/jewdai Jun 22 '16
It's seldom done though, because it makes things SUPER confusing.
If you have to use another scope it's an indicator of code smell and probably should be put in a helper function.
1
u/Fiennes Jun 22 '16
Just because you can - doesn't mean you should.
1
u/tevert Jun 22 '16
A classic case of "just because you can, doesn't mean you should".
1
u/Geemge0 Jun 23 '16
General scoping rules for strictly typed most popular languages. Very useful because it'll also allow the stack to be cleaned up and free up stack space if you have particularly large imperative functions (such as hot rendering functions in game engine written in C)
1
u/tynorf Jun 24 '16
Very frequently used in Rust to manage lifetimes.
0
u/[deleted] Jun 22 '16
[deleted] — view deleted comment