>  기사  >  백엔드 개발  >  C# 일반 컬렉션 인스턴스 적용에 대한 간략한 분석

C# 일반 컬렉션 인스턴스 적용에 대한 간략한 분석

黄舟
黄舟원래의
2016-12-21 14:41:221133검색

C# 일반 컬렉션을 이해하기 전에 컬렉션이 OOP의 중요한 개념이며 C#의 컬렉션에 대한 포괄적인 지원이 언어의 본질 중 하나라는 점을 이해합니다. C# 제네릭은 일련의 유사한 문제를 해결하는 데 주로 사용되는 C# 2.0의 새로운 요소(C++에서는 템플릿이라고 함)입니다. 이 메커니즘을 사용하면 클래스 이름을 매개변수로 일반 유형에 전달하고 해당 객체를 생성할 수 있습니다. 제네릭(클래스, 인터페이스, 메서드, 대리자 등 포함)을 템플릿으로 생각하는 것이 더 나을 수 있습니다. 템플릿의 변형 부분은 매개 변수로 전달된 클래스 이름으로 대체되어 새로운 유형 정의를 얻습니다. 제네릭은 상대적으로 큰 주제이므로 여기에서는 자세히 분석하지 않습니다. 관심 있는 사람은 관련 정보를 참조할 수 있습니다.

C# 일반 컬렉션 클래스는 사용이 매우 편리하고 빠릅니다. 이 에세이에서는 연결된 목록을 사용하여 C#에서 List﹤T﹥ 클래스의 동작을 시뮬레이션하겠습니다. 더 이상 고민하지 않고 코드에 주석이 작성되어 있으므로 추가 코드를 살펴보겠습니다. 설명:

using System.Collections;
class MyList﹤T﹥
{
PRivate MyListNode firstNode;//첫 번째 노드
private int count;//C# generic collection -Node count
public MyList()
{
this.firstNode = null;
this.count = 0;
}
//C# 일반 collection-get 목록 길이
public int GetLength()
{
return this.count;
}
//노드 추가
public void AddElement(T data)
{
MyListNode 먼저 = this.firstNode;
if(first==null)
{
this.firstNode=new MyListNode(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode(data);
this.count++;
}
//C# 일반 컬렉션 - 노드 삭제
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data ) )
{
this.firstNode = first.next;
this.count--;
return true;
}
while (first.next!=null)
{
if (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true
}
}
return false;
}
//C# 일반 컬렉션 - 지정된 인덱스에서 컬렉션 요소 가져오기
public T GetAtIndex(int ​​​​index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out ofboundary");
}
; else
{
while (innercount ﹤ 인덱스)
{
first = first.next;
innercount++;
}
return first.data;
}
}
//지정된 인덱스에 새 요소 삽입
public void InsertAtIndex(int ​​​​index,T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out ofboundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
MyListNode newNode = new MyListNode(data);
newNode.next = first.next;
first.next = newNode ;
}
this.count++;
}
//C# 일반 컬렉션 - 지정된 인덱스에서 컬렉션 요소 삭제
public void RemoveAtIndex(int ​​​​index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out ofboundary");
}
if (index == 1)
{
this.firstNode = first.next;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
first.next = first.next.next;
}
this.count--;
}
//C# 일반 컬렉션 - 컬렉션의 모든 요소 제거
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
//이 컬렉션 클래스를 구현하기 위해 foreach를 사용하여
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first!= null)
{
> Yield return first.data;
first = first.next;
}
}
//내부 노드 클래스
private class MyListNode
{
public T data { get ; set; }//노드의 요소 값

public MyListNode next { get set }//노드의 다음 노드
public MyListNode(T nodeData)
{
this .data = nodeData;
this.next = null;
}
}
}

다음은 C# generic에서 이 시뮬레이션 클래스를 사용하는 방법입니다. 컬렉션:

클래스 프로그램
{
static void Main(string[] args)
{
MyList﹤string﹥ ml = new MyList﹤string﹥();
ml.AddElement("xu" );
ml.AddElement("jin");
ml.AddElement("lin");
ml.AddElement("love");
ml. AddElement("jasmine");
ml.InsertAtIndex(4, "fiercely");
ml.RemoveAtIndex(2);
ml.Remove("lin");
foreach(문자열 s in ml)
{
Console.WriteLine(s);
}
}
}

C# 일반 컬렉션 인스턴스 애플리케이션의 기본 내용입니다. C# 일반 컬렉션 도움말을 이해하고 배울 수 있습니다.

위 내용은 C# 제네릭 컬렉션 인스턴스 애플리케이션에 대한 간략한 분석입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.