purpose for {

deanphilipowen

New member
Joined
Mar 26, 2015
Messages
1
Programming Experience
Beginner
Hello everyone,

So I'm completely new to this forum and also new to C# and coding in general.

I've bought a couple of books to get stuck in- However! I realise they teach you step by step but don't fully explain why certain "things" go where they do.

So I guess my first newbie question is

What is the main purpose for this ->

{

Feel free to laugh ;) but I learn better by breaking elements down and studying them individually.

Thanks in advance and look forward to being apart of the community.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
Braces are used in C# to define a scope. Anything defined within a particular scope is accessible to anything else inside that scope and any other scope defined within that scope. For instance:
object a;

// Code here can access `a` but no other variables.

{
    object b;

    // Code here can access `a` and `b` but no other variables.
}

{
    object c;

    // Code here can access `a` and `c`` but no other variables.

    {
        object d;

        // Code here can access `a`, `c` and `d` but no other variables.
    }
}
Certain statements introduce a scope, e.g. `if`, `for` and `using`. Anything declared in those introducing statements is considered to be within the scope, i.e. accessible inside the braces but not outside. Braces are used to define scope at the namespace, type, member and block levels.

Braces are also used to denote an array. This usage is not related to scope.
 
Top Bottom