Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
how to declare variable in c# what is difference in declaration in java script and c#
Posted

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


Javascript:

JavaScript
var myString;
//or
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.
 
Share this answer
 
Comments
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..
fjdiewornncalwe 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.
#realJSOP 5-Jul-11 10:46am    
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.
fjdiewornncalwe 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)
 
Share this answer
 
v2
Please visit following links,

Overview of C# 3.0[^]

Local variable declarations[^]

Hope it will help you :)
 
Share this answer
 
refer this[^]to get clear view.

hope it helps :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900