C# की मूल बातें

ऐरे

ऐरे एक ही टाइप के तय संख्या में मान रखता है, और हर मान 0 से शुरू होने वाले इंडेक्स से मिलता है।

यह कैसे काम करता है

C# ऐरे बनाएँ, पढ़ें, बदलें, उन पर चलें और सॉर्ट करें, 2D ऐरे भी।

Length आकार बताता है और ^1 आख़िरी एलिमेंट पढ़ता है। foreach हर एलिमेंट पर जाता है, Array.Sort उसी जगह सॉर्ट करता है, new int[3] शून्य से शुरू होता है, और int[,] पंक्ति और कॉलम वाली ग्रिड बनाता है।

देखें यह कैसे काम करता है

इंडेक्स चुनने के लिए किसी खाने पर क्लिक करें या स्लाइडर खिसकाएँ। जोड़ें या हटाएँ और देखें कि बाद के सभी तत्व क्यों खिसकते हैं।

चलने वाला C# उदाहरण

C#
कोड चलाएँ →
Program.cs
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        // An array has a fixed length set when it is created.
        int[] scores = { 72, 95, 88, 64 };
        Console.WriteLine($"Length: {scores.Length}");
        Console.WriteLine($"First: {scores[0]}, last: {scores[^1]}");

        scores[3] = 70;  // elements can be changed, the length cannot

        int sum = 0;
        foreach (int score in scores)
        {
            sum += score;
        }
        Console.WriteLine($"Sum: {sum}, average: {sum / (double)scores.Length}");

        // Array.Sort sorts in place; string.Join prints the elements.
        Array.Sort(scores);
        Console.WriteLine($"Sorted: {string.Join(", ", scores)}");
        Console.WriteLine($"Max: {scores.Max()}");

        // new int[3] creates an array of zeros.
        int[] counts = new int[3];
        counts[1] = 5;
        Console.WriteLine($"Counts: {string.Join(" ", counts)}");

        // A 2D array is indexed by row and column.
        int[,] grid = { { 1, 2, 3 }, { 4, 5, 6 } };
        Console.WriteLine($"grid[1, 2] = {grid[1, 2]}");
    }
}
अपेक्षित आउटपुट
Length: 4
First: 72, last: 64
Sum: 325, average: 81.25
Sorted: 70, 72, 88, 95
Max: 95
Counts: 0 5 0
grid[1, 2] = 6

डिबगर से अभ्यास करें

किसी अहम असाइनमेंट या शर्त पर ब्रेकपॉइंट लगाएँ, Debug दबाएँ और अगली लाइन पर जाने से पहले लोकल मान देखें।

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज