Is there any difference between,

 using (SPSite site = new SPSite("http:\sdkfsdhfskdjf"))
 using (SPWeb web = site.OpenWeb())


using (SPSite site = new SPSite("http:\sdkfsdhfskdjf"))
 using (SPWeb web = site.OpenWeb())

2 Answers 2


No. If you have just one operation brackets are not necessary. It's like with "if" operator.

  • 1
    Using brackets all the way just makes it more readable and less confusing.
    – user2536
    Aug 22, 2013 at 13:58
  • It's just style question. In one side it's realy less confusing. In other it's 2 additional rows of code. Code style is the big source of holivar :)
    – IAfanasov
    Aug 22, 2013 at 17:31

to my understanding both are the same! but with a big but!

having code only executed within the SPSite wouldnt be an option in the first instance as your stacking the using statment and the second it would work!

either way is fine but the first can be prone to errors! both will be disposed as both are encapsulated within the using statment! The compiler at comile time will add in the backets even tho they are ommitted in code!

its not the brackets thats the issue its resuablity and maintainability! by that i mean you work on a project and dont put in curly brackets... you complete the project and leave the company, the company then goes on to hire someone else to ammend the code but easily make a simple mistake as he is human afterall and going through thousands of lines of code can get confusing! so its there inplace to stop the confusion with adding another line to the statment when it shouldnt be there!

nested if statments can get big and can get very confusing in complex applications, removing the brackets "can" only lead(s) to possible confusion! dont forget you might understand the code and know where/what is going on but the next person to work on it I bet wouldnt and would need time to understand it! In a business its crutial to have little room for development errors and bugs especialy in crutial systems like planes traffic lights ect that involves many lives!

for me, debug code is fine to ommit the brackets but for business code and especialy crutial systems I would have them in.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.