چک کردن وجود یک object در ArrayList با استفاده از متد Contains در #C
سلام بر تمامی برنامه نویسان سی شارپ 😉 در این مطلب ، چک کردن وجود یک object در ArrayList با استفاده از متد Contains در سی شارپ #C رو براتون آماده کردم. انشاالله که این کد براتون مفید باشه.
خروجی پروژه به این صورت می باشد:
Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList does contain myB
myB occurs at index 1
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList does contain myB
myB occurs at index 1
چک کردن وجود یک object در ArrayList با استفاده از متد Contains :
using System;
using System.Collections;
class Employee : IComparable
{
public string Name;
public int Number;
public Employee(string Name, int Number)
{
this.Name = Name;
this.Number = Number;
}
public override string ToString()
{
return "Name is " + Name + ", Number is " + Number;
}
public int Compare(object lhs, object rhs)
{
Employee lhsEmployee = (Employee) lhs;
Employee rhsEmployee = (Employee) rhs;
if (lhsEmployee.Number < rhsEmployee.Number) {
return -1;
} else if (lhsEmployee.Number > rhsEmployee.Number) {
return 1;
} else {
return 0;
}
}
public int CompareTo(object rhs) {
return Compare(this, rhs);
}
}
class MainClass
{
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
for (int i = 0; i < myArrayList.Count; i++) {
Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
}
}
public static void Main()
{
ArrayList myArrayList = new ArrayList();
// add four Employee objects to myArrayList using the Add() method
Console.WriteLine("Adding four Employee objects to myArrayList");
using System.Collections;
class Employee : IComparable
{
public string Name;
public int Number;
public Employee(string Name, int Number)
{
this.Name = Name;
this.Number = Number;
}
public override string ToString()
{
return "Name is " + Name + ", Number is " + Number;
}
public int Compare(object lhs, object rhs)
{
Employee lhsEmployee = (Employee) lhs;
Employee rhsEmployee = (Employee) rhs;
if (lhsEmployee.Number < rhsEmployee.Number) {
return -1;
} else if (lhsEmployee.Number > rhsEmployee.Number) {
return 1;
} else {
return 0;
}
}
public int CompareTo(object rhs) {
return Compare(this, rhs);
}
}
class MainClass
{
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
for (int i = 0; i < myArrayList.Count; i++) {
Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
}
}
public static void Main()
{
ArrayList myArrayList = new ArrayList();
// add four Employee objects to myArrayList using the Add() method
Console.WriteLine("Adding four Employee objects to myArrayList");