Solved

C# using { brackets

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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