Click here to Skip to main content
13,446,125 members (36,592 online)
Rate this:
Please Sign up or sign in to vote.
See more:
how to declare variable in c# what is difference in declaration in java script and c#
Posted 5-Jul-11 2:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

string myString;
// or
string myString = "value";


var myString;
var MyString="value";

In C# you must specify the type, but in Javascript, the act of assigning a value defines a variable's type. You can also specify the type explicitly if you want.
JV9999 5-Jul-11 9:00am
in C# you don't need to specify the type either when you are using C# 3.0 or higher during development. You can simply use "var" and the compiler which get you the right type..
Marcus Kramer 5-Jul-11 9:35am
Many highly experienced developers including myself would discourage the use of var. Explicitly defining the type both in the variable declaration and initialization provides a second level of type validation, especially in the case of the example John gives using string.
I've been coding a LONG time. I don't believe in "typeless" languages, and would never avoid specifying the desired type. For work, I code in VB.Net (but only because I'm forced to) and I refuse to let the compiler assume what I really want to do. I even scope all of my If statements with parenthesis, which drives my co-workers completely insane.
Marcus Kramer 5-Jul-11 23:19pm
Finally, someone else who annoys the hell out of coworkers with these behaviors. I do the same thing.
JV9999 5-Jul-11 11:28am
I personally agree with you both (I indeed didn't mention that), but personal preferences and thoughts on best practises in coding is not what he asked :), so the general statement that it's not possible is incorrect, but both of you are completely correct that you should avoid using it. (Revoted 5)
Marcus Kramer 5-Jul-11 23:21pm
I apologize if my comment was taken as a slight against your answer. It was not intended as that. I was just offering my personal position on the use of var for the OP the chew on. Cheers.
JV9999 7-Jul-11 2:25am
For my comments goes the same :). I guess the problem with these kind of questions are the kind of correct answers you have. You have the Microsoft-correct answers, but also the Community-correct answers. Which answer is the one the author wants? ;) But now we have given Shashikant@Jalgaon both, so he can pick his prefered one.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Please visit following links,

Overview of C# 3.0[^]

Local variable declarations[^]

Hope it will help you :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

refer this[^]to get clear view.

hope it helps :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180314.2 | Last Updated 16 Jul 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100