Solved

C# using { brackets

Posted on 2012-03-20
5
233 Views
Last Modified: 2012-03-20
Hi

I am a VB.net developer starting C#
I just wanted a guideline on how the { brackets work. I noticed that they autogenerate
ig I double click on a button. Can someone give me a few guidelines on using them in stand alone procedures etc - in relation to VB.net
0
Comment
Question by:murbro
5 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 250 total points
ID: 37742265
Brackets are basically block delimiters

They say, there is body to be executed here.
So

if  whateve
{ dosomething;
   dosomethingelse;
   dosomemore;
}
else
{ dothisinstead;
   andthins;
}

same for whiles
same for select statement which is called switch()
save for  procedure and function bodies.

int square(int a) {
      return a * a;
}

You can sometimes write a one line If where there is only one statement and avoid {}, but
it is more readable to just use them.  Of course some of the above is just pseudo code.
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 250 total points
ID: 37742300
VB
if x then
    do
    d2
    d3
    d4
else
    do5
    do 6
end if

C
if (x)
{
    do;
    d2;
    d3;
    d4;
}
else
{
    do5;
    do6;
}
//notice, no end if in c#

c languages allow multiple line spans without a line continuation char
vb.net
dim s as string="this is some text" & _
                            "with more text on the second line"

c
string s =new string("this is some text
                                     with more text on the second line");

//that's why c/C++/C# need the ;

Well something like that. I don't program in C#.. Hope this helps.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 125 total points
ID: 37742311
{} or curly brackets can be seen as a Begin and End statement that surround blocks of code so as shown below they surround the code for a class, and a method/function and surround the code for say a foreach loop

class Program
{
    static int[] _array = { 1, 2 };

    static void Main()
    {
	Good();
    }

    static void Good()
    {
	foreach (int element in _array)
	{
	    Console.WriteLine(element);
	    Console.WriteLine(true);
	}
    }
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 37742344
Hopefully you won't encounter it often, but be aware that you can also have brackets not associated with anything.

e.g

{
    int x = 4;
}

Open in new window


In this way, you have created a block (scoping) where x only exists within the brackets. This usage would be a way for you to show in your code that x is only used in this one spot. It's not a very common usage, AFAIK, but if you run across it in usage, at least you'll be aware. The closest thing in VB that I can think of would be a With block, if you structured it where the target of the With was acquired in the With declaration.

e.g.

With oCmd.OpenRecordset(...)
    ...
End With

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 37742431
Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question