IComparable.CompareTo(Object) 메서드
Unity3D/C# 2021. 3. 16. 17:52docs.microsoft.com/ko-kr/dotnet/api/system.icomparable.compareto?view=net-5.0
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
namespace Test
{
public class User{
public string name;
}
class Program
{
static void Main(string[] args)
{
// new App();
//new Test();
var item1 = new Item("장검", Item.eItemType.WEAPON, Item.eGrade.MAGIC);
var item2 = new Item("단검", Item.eItemType.WEAPON, Item.eGrade.MAGIC);
var result = item1.CompareTo(item2);
Console.WriteLine(result);
}
}
}
using System;
namespace Test
{
public class Item : IComparable
{
public enum eItemType
{
WEAPON, ARMOR, ACCESSORY, POTION
}
public enum eGrade
{
NORMAL, MAGIC, LEGEND
}
private string name;
private eItemType itemType;
private eGrade grade;
//생성자
public Item(string name, eItemType itemType, eGrade grade = eGrade.NORMAL)
{
this.name = name;
this.itemType = itemType;
this.grade = grade;
}
public string GetName()
{
return this.name;
}
public eItemType GetItemType()
{
return this.itemType;
}
public eGrade GetGrade(){
return this.grade;
}
public int CompareTo(object obj) {
if (obj == null) return 1;
Item other = obj as Item;
if (other != null)
return this.grade.CompareTo(other.GetGrade());
else
throw new ArgumentException("Object is not a Temperature");
}
}
}
'Unity3D > C#' 카테고리의 다른 글
싱글톤(Singletone) vs C#정적클래스(static class) 차이점 (0) | 2021.03.25 |
---|---|
2048 (0) | 2021.03.19 |
c# async await (0) | 2021.03.10 |
.NET의 \0(U + 0000) 과 String (0) | 2020.12.02 |
C# bitwise (0) | 2020.11.27 |