Solved

C# using { brackets

Posted on 2012-03-20
5
229 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 74

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now