Output the indexer taught in the past
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