Press "Enter" to skip to content

C# Array

Array is the structure which keep the variables consecutively on the ram. Arrays are important for the all programing languages. With C programming language, I made the some programs and we can make these programs with C#. For the examples click here.

Firstly, I will make fibonacci loops using with the arrays.  Code Sequence;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
       

        
        static void Main(string[] args)
        {
            // We define the array after the type of the variable
            int[] fibonacci = new int[10];
            fibonacci[0] = 1;
            fibonacci[1] = 1;
            for (int i = 2; i < 10; i++)
            {
                fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
            }
            // I take the length of array using with length term
            for (int i = 0; i < fibonacci.Length; i++)
            {
                Console.WriteLine(fibonacci[i]);
            }
            Console.ReadKey();
        }
    }
}

Screen Output;

1
1
2
3
5
8
13
21
34
55

Now i will define a class and this class name is will be school. In this school there will be 3 student. Program will keep the these student’s names,surnames and school numbers and then program will press the screen. Code Sequence;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        
            class school
            {
                string name;
                string surname;
                int schoolnumber;




                public school(string name, string surname, int schoolnumber)
                {
                    this.name = name;
                    this.surname = surname;
                    this.schoolnumber = schoolnumber;
                }
                override public string ToString()
                {
                    return ("" + name + " " + surname + " " + schoolnumber);
                }
                // We have to write this method if we want to get screen output.
            }


            static void Main(string[] args)
            {
                school[] students = new school[3];
                students[0] = new school("Ebubekir", "Sezer", 49);
                students[1] = new school("Ben", "Davies", 56);
                students[2] = new school("Victor", "Kocaman", 93);
                for (int i = 0; i < students.Length; i++)
                {
                    Console.WriteLine(students[i]);
                }
                Console.ReadKey();
            }
        }
    }

Screen Display;

Ebubekir Sezer 49
Ben Davies 56
Victor Kocaman 93

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *