I tried to chew C # (indexer)

Output the indexer taught in the past

Supplement 1

Not in Java

Program.cs

using System;
using System.Collections.Generic;

namespace IndexerLesson
{
    public class Colors
    {
        private string[] data = { "Red", "Blue", "yellow" };
       
         //Access return value this[Type argument]
        public string this[int index]{
            set{
                this.data[index] = value;
            }
            get{
                return data[index];
            }
        }

    }
}

Colors.cs

using System;
using System.Collections.Generic;

namespace IndexerLesson
{
    public class Colors
    {
        private string[] data = { "Red", "Blue", "yellow" };

        public string this[int index]
        {
            set
            {
                this.data[index] = value;
            }
            get
            {
                return data[index];
            }
        }

    }
}

Overloadable

JMonth.cs

using System;
using System.Collections.Generic;

namespace IndexerLesson
{
    public class JMonth
    {
        private string[] months = { "January", "February", "Yayoi", "Uzuki", "Satsuki", "Minazuki", "July", "Hazuki", "Nagatsuki", "Kannazuki", "Shimotsuki", "December" };
        public string this[int index]
        {
            get
            {
                return months[index - 1];
            }
        }
        public int this[string name]
        {
            get
            {
                return Array.IndexOf(months, name) + 1;
            }
        }

    }
}

Profram.cs

using System;
using System.Collections.Generic;

namespace IndexerLesson
{
    class Class1
    {
        static void Main(string[] args)
        {
            JMonth jMonth = new JMonth();
            Console.WriteLine(jMonth[6]);
            Console.WriteLine(jMonth["Kannazuki"]);
        }
    }
}

It is recognition.

Recommended Posts

I tried to chew C # (indexer)
I tried to chew C # (polymorphism: polymorphism)
I tried to chew C # (basic of encapsulation)
I tried to chew C # (reading and writing files)
I tried to verify yum-cron
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to explain Active Hash
I tried connecting to Oracle Autonomous Database 21c with JDBC Thin
I tried to generate a C language program source from cURL
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I also tried WebAssembly with Nim and C
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)