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

C# में ऐरे के पहले एलिमेंट का इंडेक्स कैसे खोजें

ऐरे के पहले एलिमेंट का इंडेक्स प्राप्त करने के लिए Array.GetLowerBound(0) मेथड का उपयोग करें।

सिंटैक्स

public int GetLowerBound(int dimension);

पैरामीटर dimension शून्य-आधारित डायमेंशन है (0 = पहला डायमेंशन)।

उदाहरण 1: एलिमेंट्स के साथ ऐरे

using System;

public class Demo {
    public static void Main() {
        string[] products = new string[] { "Andy", "Mark", "Gary", "Andre" };
        
        Console.WriteLine("One or more name begin with 'A'? = {0}", 
            Array.Exists(products, ele => ele.StartsWith("A")));
        Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
        Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
        Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
        Console.WriteLine("Index of the 1st element = " + products.GetLowerBound(0));
    }
}

आउटपुट

One or more name begin with 'A'? = True
Is the array having fixed size? = True
Is the array read only? = False
Is the array synchronized? = False
Index of the 1st element = 0

उदाहरण 2: खाली ऐरे

using System;

public class Demo {
    public static void Main() {
        string[] products = new string[] { };
        
        Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
        Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
        Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
        Console.WriteLine("Index of the 1st element = " + products.GetLowerBound(0));
    }
}

आउटपुट

Is the array having fixed size? = True
Is the array read only? = False
Is the array synchronized? = False
Index of the 1st element = 0

महत्वपूर्ण बिंदु

  • C# में ऐरे हमेशा शून्य-आधारित होते हैं → GetLowerBound(0) हमेशा 0 लौटाता है
  • यह मेथड मल्टीडायमेंशनल ऐरे के लिए अधिक उपयोगी है
  • GetUpperBound(dimension) अंतिम इंडेक्स लौटाता है
  • Length प्रॉपर्टी कुल एलिमेंट्स देता है

मल्टीडायमेंशनल ऐरे उदाहरण

int[,] matrix = new int[3, 4];  // 3 पंक्तियाँ, 4 कॉलम

Console.WriteLine("Dimension 0 lower: " + matrix.GetLowerBound(0));  // 0
Console.WriteLine("Dimension 0 upper: " + matrix.GetUpperBound(0));  // 2
Console.WriteLine("Dimension 1 lower: " + matrix.GetLowerBound(1));  // 0
Console.WriteLine("Dimension 1 upper: " + matrix.GetUpperBound(1));  // 3

// कुल एलिमेंट्स
Console.WriteLine("Total elements: " + matrix.Length);  // 12

ऐरे गुण सारांश

गुण/मेथडविवरणउदाहरण मान
GetLowerBound(0)पहले डायमेंशन का पहला इंडेक्स0
GetUpperBound(0)पहले डायमेंशन का अंतिम इंडेक्सLength - 1
Lengthसभी डायमेंशन्स में कुल एलिमेंट्स12
Rankडायमेंशन्स की संख्या2
IsFixedSizeहमेशा true (C# ऐरे)True
IsReadOnlyहमेशा falseFalse
IsSynchronizedथ्रेड-सेफ है?False
  1. - C# में StringCollection को ऐरे में निर्दिष्ट इंडेक्स पर कॉपी करना

    StringCollection.CopyTo() मेथड स्ट्रिंग कलेक्शन के सभी तत्वों को एक स्ट्रिंग ऐरे (string[]) में निर्दिष्ट इंडेक्स से कॉपी करता है। StringCollection System.Collections.Specialized नेमस्पेस में है। उदाहरण 1: सटीक आकार के ऐरे में कॉपी करना (इंडेक्स 0) using System; using System.Collections.Specialized

  1. - C# प्रोग्राम: सरणी से पहला तत्व प्रदर्शित करना

    निम्नलिखित हमारी सरणी है:double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};पहला तत्व प्राप्त करने के लिए, First() मेथड का उपयोग करें।myArr.AsQueryable().First();आइए पूरा कोड देखें:उदाहरणusing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() { &n

  1. - Python - K से बड़े पहले तत्व का इंडेक्स प्राप्त करना

    Python सूची में आइटम के मान आवश्यक रूप से किसी क्रमबद्ध क्रम में नहीं होते हैं। इसके अलावा ऐसी स्थिति हो सकती है जब हम केवल एक विशिष्ट मान से बड़े मानों में रुचि रखते हैं। इस लेख में हम देखेंगे कि हम कैसे प्राप्त कर सकते हैं Enumeration का उपयोग करना Enumeration का उपयोग करके हमें सूची में तत्वों क