← Back to C# Course | Chapter 2: Variables & Types | Lesson 1 of 7

var Keyword क्या है

var keyword C# को यह पता लगाने देता है कि आप जो उसमें डालते हैं उसे देखकर एक variable किस तरह की चीज़ रखता है। यह एक labeler जैसा है जो item पढ़ता है और आपके लिए सही label लिखता है।

In this page:

  1. The var Keyword
Syntax
csharp
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

markup
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());
    }
}
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. #}
आम गलतियां
  1. बिना initial value के var x; declare करना, जो एक error है क्योंकि compiler type infer नहीं कर सकता।
  2. बाद में किसी दूसरे type की value assign करना, जैसे var n = 5; n = "five";, जो fail होता है क्योंकि n एक int है।
  3. var का ज़्यादा उपयोग करना जब right-hand side से type स्पष्ट न हो, जो code को पढ़ना मुश्किल बनाता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.