Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Const vs Static vs Readonly


Const

Constant Fields वे Fields हैं जिन्हें Modified नहीं किया जा सकता। Declaration के समय, आपको इसमें Value Assign करनी होगी।

const int a = 5;

Static

यदि Static Modifier Class पर Apply किया जाता है तो आप New Keyword का उपयोग करके Class को Instantiate नहीं कर सकते। Methods, Properties, Classes, Constructors आदि पर Static Keyword का उपयोग कर सकते हैं।

static int a = 10;

Readonly

Readonly Field को Declaration के समय Initialize किया जाता है या आप इसे Constructor के भीतर भी Set कर सकते हैं।

आइए एक Example देखते हैं जिसमें Readonly Field को Constructor के अंदर Initialize किया गया है।

Example

class Demo {    readonly int a;    public Demo() {       a = 5;    } }

  1. JavaScript Const: ब्लॉक-स्कोप्ड कॉन्स्टेंट्स JavaScript Const: ब्लॉक-स्कोप्ड कॉन्स्टेंट्स

    const ES2015 (ES6) में पेश किया गया एक ब्लॉक-स्कोप्ड वेरिएबल डिक्लेरेशन है जो रीअसाइनमेंट को रोकता है। यह आधुनिक JavaScript में डिफ़ॉल्ट विकल्प होना चाहिए जब रीअसाइनमेंट की आवश्यकता न हो। उदाहरण { try { a = 44; // TypeError: Assignment to constant variable } catch (e

  1. JavaScript में Const vs Let JavaScript में Const vs Let

    Const और let को ES2015 में ब्लॉक-स्कोप्ड वेरिएबल घोषित करने के लिए पेश किया गया था। जबकि let का उपयोग करके घोषित वेरिएबल को पुनः असाइन किया जा सकता है, const का उपयोग करके घोषित वेरिएबल को पुनः असाइन नहीं किया जा सकता। उदाहरण <!DOCTYPE html> <html lang="en"> <head> <me

  1. CSS position: static CSS position: static

    position: static (डिफ़ॉल्ट) position: static एलिमेंट्स का डिफ़ॉल्ट पोजिशनिंग व्यवहार है। स्टेटिक पोजिशन्ड एलिमेंट्स नॉर्मल डॉक्यूमेंट फ्लो के अनुसार प्रदर्शित होते हैं। top, right, bottom, left, और z-index प्रॉपर्टीज का उन पर कोई प्रभाव नहीं पड़ता। उदाहरण <!DOCTYPE html> <html> <he