C#学习(http://www.c-sharpcorner.com/UploadFile/Saurabh.Mishra/GenericsInCsharp204172006013416AM/Generi...

 

None.gif using  System;
None.gif
None.gif
using  System.Collections.Generic;
None.gif
None.gif
using  System.Text;
None.gif
None.gif 
None.gif
None.gif
namespace  GenericsSample
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
class Person
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
int _Age;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public int Age
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Age; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Age = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        String _Name;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public String Name
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Name; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Name = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        String _Address;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public String Address
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Address; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Address = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        String _Company;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public String Company
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Company; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Company = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Person() dot.gif{ }
InBlock.gif
InBlock.gif        
public Person(String Name)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
this.Name = Name;
InBlock.gif
InBlock.gif            
this.Age = 0;
InBlock.gif
InBlock.gif            
this.Address = String.Empty;
InBlock.gif
InBlock.gif            
this.Company = String.Empty;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Person(String Name, int Age, String Address)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
this.Name = Name;
InBlock.gif
InBlock.gif            
this.Age = Age;
InBlock.gif
InBlock.gif            
this.Address = Address;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
class Program
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
static void Main(string[] args)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif                   
//Generic List Creation
InBlock.gif
InBlock.gif            
//List is a Generic Class provided by .Net Framework 2.0
InBlock.gif
InBlock.gif            
//System.Collections.Generics is the Namespace.
InBlock.gif

InBlock.gif 
InBlock.gif
InBlock.gif            List
<Person> myPerson = new List<Person>();
InBlock.gif
InBlock.gif            myPerson.Add(
new Person("Saurabh"));
InBlock.gif
InBlock.gif            myPerson.Add(
new Person("Manu"));
InBlock.gif
InBlock.gif            myPerson.Add(
new Person("SomeOne"24"Gurgaon"));
InBlock.gif
InBlock.gif            myPerson.Add(
new Person("SomeoneElse"24"Gurgaon"));
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
//myPerson.Add(new Car());// This is A Compile Time Error
InBlock.gif

InBlock.gif 
InBlock.gif
InBlock.gif            
foreach (Person p in myPerson)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                Console.WriteLine(p.Name);
InBlock.gif
InBlock.gif                Console.WriteLine(p.Age);
InBlock.gif
InBlock.gif                Console.WriteLine(p.Address);
InBlock.gif
InBlock.gif                Console.WriteLine(p.Company);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.ReadLine();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/longren629/archive/2006/04/20/380211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值