You are on page 1of 6

c  

„   
  
 
  
 
  

„  
 

 
  
  

   !"#$$  
 
     % 
 


 




 
%

 T  
 
MyList<T>


 
MyList<int> MyList<string> MyList<MyClass>  

 
  % &
 


'
 
   
 (
  


„  
)* 
 
%
   
#$$

+
)* „  
 
 
 ,
System.Collections.Generic  

 
 
List Queue Stack LinkedList
 
   

 
  

º

  


 % )* 
,
  %
 

 
%System.Object(
  
  





 
#-
 

%
 
 ArrayList 
 „  %%ArrayList



 

%  
 
  


 

// The .NET Framework 1.1 way of creating a list

ArrayList list1 = new ArrayList();


list1.Add(3);
list1.Add(105);
//...

ArrayList list2 = new ArrayList();


list2.Add("First item.");
list2.Add("Second item");
//...




  .

  
ArrayList
  System.Object    %% & 

  

% & 

 % &


% &


  
/ % &


% &

%




      

Π  
 &% ArrayList% % 

 
    
%    

% 
 
.
ArrayList  ! 

 (
  

   % 0%1

 
%    




List<T> 

System.Collections.Generic
   

 
   
 2

# 
// The .NET Framework 2.0 way of creating a list

List<int> list1 = new List<int>();


list1.Add(3); // No boxing, no casting

list1.Add("First item"); // Compile-time error

„ 
  

& List<T>  ArrayList
 









  

 &  



ArrayList % 


  
 

 

(

 

 
 

 
  

 %  
 
 
  


 
   

  
      %
 

public class Node <T>


{
T head;
T next;
}

ü T 3



 


%

2

Node<string> node = new Node<string>();

    



& string
 
string  
% 


   
.
      
void Swap<T>( ref T left, ref T right)
{
T temp;
temp = left;
left = right;
right = temp;
}

  
 &    %   2

int a = 1;
int b = 2;
Swap <int> (a, b);

4 
   %   
  
  
     2

Swap (a, b);

   



  
& 
  
  



& 
%  

 

 2

using System;
using System.Collections.Generic;
public class MyList<T> //type parameter T in angle brackets

{
private Node head;

// The nested class is also generic on T.

private class Node


{
private Node next;
//T as private member data type:

private T data;
//T used in non-generic constructor:

public Node(T t)
{
next = null;
data = t;
}

public Node Next


{
get { return next; }
set { next = value; }
}

//T as return type of property:

public T Data
{
get { return data; }
set { data = value; }
}
}

public MyList()
{
head = null;
}

//T as method parameter type:

public void AddHead(T t)


{
Node n = new Node(t);
n.Next = head;
head = n;
}

public IEnumerator<T> GetEnumerator()


{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}

 
 % 2

public class MyList<T>

T   %    NodeT




int string 
 &%   
  
  

  
 &   
 
MyList<T> 
 
Π%

 
  % 
%
  string 
  2

class Program
{
static void Main(string[] args)
{
//int is the type argument.

MyList<int> list = new MyList<int>();

for (int x = 0; x < 10; x++)


list.AddHead(x);

foreach (int i in list)


Console.WriteLine(i);

Console.WriteLine("Done");
}
}

0 
  
 
%
!.
%     
 
#$$ 
  

ü  
 
    
3

    
 'Π5 

 


 ü 'Œ 5 



%

 
  


3


   
 
,
    %
 

'Œ 5Œ,

  

 

„ &       Stack


 
 2

Stack<int> stack;

. 
 
 
,
 Stack 

%    
     

 
 
,Stack
  
&   


  
 



 Stack<int>
 2

Stack<int> stackOne = new Stack<int>();


Stack<int> stackTwo = new Stack<int>();

ü  
 

    
Stack% 

long 6
  

 



 
  %
long
 

'Π5#




%,





(
 %
 
 




 
 ,
  %1

%  
'Π5
 




    
  
,
 
 %%

 ,

„ &     


 Customer

Order 

  Customer2

Stack<Customer> customers;

. 
 
 
,
 Stack 
 
 
   %1
 %
Π
&
  
 

 Order2

Stack<Order> orders = new Stack<Order>();

7
 
,
 Stack
 
Order8 


 ,
 Stack

% 
Π 

 

   
 Customer2

customers = new Stack<Customer>();

.   Stack Order 




 
,Stack 
 



 


  , Customer#-  

 
 
% %

 % ,%  
 

 1


ü     


 
#-(

#$$ % 
   
 ,ü   

% 
 .
& 
#-
    ,  
  & 
#$$ 

#- 
   

#$$
  

.  

  
#-
%

  
 


  
 

 %1