Solved

C# using { brackets

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

20 Experts available now in Live!

Get 1:1 Help Now