const Fields कैसे काम करते हैं
एक const permanent marker से लिखी एक value है: आप इसे एक बार सेट करते हैं और यह कभी नहीं बदल सकती। यह हफ़्ते में दिनों की संख्या जैसे facts के लिए perfect है।
In this page:
Syntax
const type ConstantName = value; // must be known at compile time
const Fields
एक const field एक compile-time constant है — इसकी value compile time पर ज्ञात होनी चाहिए और बाद में कभी नहीं बदल सकती।
readonly के विपरीत, const values compiled code में जहां भी उपयोग हों वहां सीधे substitute हो जाती हैं। const mathematical constants या fixed configuration limits जैसी values के लिए आदर्श है।
Warning:
किसी
const को किसी method call का result assign नहीं किया जा सकता, क्योंकि इसकी value compile time पर resolvable होनी चाहिए।उदाहरण: const Fields
using System;
class Program
{
const double Pi = 3.14159;
const int MaxRetries = 3;
static void Main(string[] args)
{
Console.WriteLine(Pi);
Console.WriteLine(MaxRetries);
Console.WriteLine(Pi * 2 * 5);
}
}
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. #}
आम गलतियां
- किसी
constको declare करने के बाद उसे assign करने की कोशिश करना, जो एक compile error है। - बिना value के, या compile time पर अज्ञात value के साथ, एक
constdeclare करना। - runtime पर compute होनी चाहिए वाली value के लिए
constका उपयोग करना, जबकिreadonlyसही choice है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: