471,337 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Assigning values to variables

In C#, i have seen values being assigned to strings in 2 ways.
1) string name = "Bill Gates";
2) string name = ("Bill Gates");

what is the difference betn. the 2 methods if any?
the second method uses parentheses. does this have any special influence on
the value of the string?

Any help would be welcome.

Regards,
Kiran
Feb 3 '06 #1
3 1547
No difference in your example.

Generally used in arithmetic operations, where brackets take
precedence.

Also applies a measure of scope.
For example, if I have an object called obj....

((Button)obj).Text = "Start"; // This would work
((Button)obj.Text) = "Start"; // This would not because its trying to
cast (obj.Text) instead of (obj).

Feb 3 '06 #2
I have never seen anyone use the second form when dealing with strings.

The parentheses won't do any harm to the code, but they will give pause
to another programmer reading the code, who will stop and think, "What
the...?" before realizing that it doesn't matter and moving on.

Stick with the first form. :)

Feb 3 '06 #3
Kiran A K wrote:
In C#, i have seen values being assigned to strings in 2 ways.
1) string name = "Bill Gates";
2) string name = ("Bill Gates");

what is the difference betn. the 2 methods if any?


no difference.

in this case extra parens are not helping, though i
frequently use them to explictly set precedence in
arithmetic expressions.

hth,
esmail
Feb 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ben | last post: by
14 posts views Thread by Eric Bantock | last post: by
8 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
37 posts views Thread by miken32 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.