var Keyword क्या है
var keyword C# को यह पता लगाने देता है कि आप जो उसमें डालते हैं उसे देखकर एक variable किस तरह की चीज़ रखता है। यह एक labeler जैसा है जो item पढ़ता है और आपके लिए सही label लिखता है।
In this page:
Syntax
var variableName = initialValue; // type is inferred at compile time
The var Keyword
var compiler को किसी variable के type को compile time पर उसके initializer से infer करने देता है। variable फिर भी statically typed है — var dynamic typing नहीं है, यह programmer के लिए सिर्फ एक shorthand है।
इसे हमेशा उसी line पर initialize होना चाहिए जिस पर यह declared है।
Warning:
बिना initializer के
var x; एक compile error है — compiler के पास type infer करने के लिए कुछ नहीं है।उदाहरण: The var Keyword
using System;
class Program
{
static void Main(string[] args)
{
var count = 10;
var name = "Alice";
var price = 19.99;
Console.WriteLine(count.GetType());
Console.WriteLine(name.GetType());
Console.WriteLine(price.GetType());
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- बिना initial value के
var x;declare करना, जो एक error है क्योंकि compiler type infer नहीं कर सकता। - बाद में किसी दूसरे type की value assign करना, जैसे
var n = 5; n = "five";, जो fail होता है क्योंकिnएकintहै। varका ज़्यादा उपयोग करना जब right-hand side से type स्पष्ट न हो, जो code को पढ़ना मुश्किल बनाता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: