C # use of generic collection classes (Ilist, IDictionary)

Source: Internet
Author: User

IDictionary is the base interface of the generic set of key/value pairs. Each element has a key-value pair in the keyValuepair object.

Each pair must have a unique key. Whether to allow null reference of key (Nothing in Visual Basic) is different. This value can be a null reference (Nothing in Visual Basic) without being unique.IDictionaryThe interface allows enumeration of key and value, but this does not mean any specific sorting order.

C #ForeachStatement (in Visual BasicFor EachIn C ++For each) The type of each element in the collection. BecauseIDictionaryEach element of is a key/value pair, so the element type is neither the key type nor the value type. InsteadKeyValuePairType.

 

The following is a simple example program.

 

Entity class:

Namespace Domain
{

// Order
Public class Order
{
Public int OrderID {get; set ;}

/// <Summary>
/// Order Date
/// </Summary>
Public DateTime OrderDate {get; set ;}

/// <Summary>
/// Order address
/// </Summary>
Public string OrderAdress {get; set ;}

/// <Summary>
/// Order phone number
/// </Summary>
Public string OrderTel {get; set ;}
}
}

 

// Order Details


Namespace Domain
{
Public class OrderDetail
{
Public int DetailID {get; set ;}

Public int ProductID {get; set ;}

/// <Summary>
/// Quantity
/// </Summary>
Public decimal Quantity {get; set ;}

/// <Summary>
/// Unit price
/// </Summary>
Public decimal Price {get; set ;}
}
}

 

// Store data

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Domain;

Namespace Service
{
Public static class OrderStock
{
Private static IList <Order> m_orderList = new List <Order> (); // defines the generic field of a List.

Public static IList <Order> OrderList // defines the generic attribute of a List.
{
Get
{
Return m_orderList;
}
Set
{
M_orderList = value;
}
}

Private static IDictionary <Order, IList <OrderDetail> m_orders = new Dictionary <Order, IList <OrderDetail> ();

// Defines a generic field of Dictionary,

Public static IDictionary <Order, IList <OrderDetail> Orders/defines a generic attribute of a Dictionary.
{
Get
{
Return m_orders;
}
Set
{
M_orders = value;
}
}
}
}

 

Service Interface

Using System;
Using Domain;
Namespace Service
{
Public interface IOrderService
{

// Delete
Void Delete (Domain. Order entity );

// Query all
System. Collections. Generic. IList <Domain. Order> LoadAll ();

// Save
Object Save (Domain. Order entity );

// Update
Void Update (Domain. Order entity );

// Query by ID
Order Get (object id );

// Obtain the next ID
Object GetNextID ();
}

 

Namespace Service
{
Public interface IOrderDetailService
{
Void Delete (System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> entity );
System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> Get (object id );
Object GetNextDetailID (object mainID );
Object GetNextID ();
System. Collections. Generic. IDictionary <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> LoadAll ();
Object Save (Domain. Order entity, System. Collections. Generic. IList <Domain. OrderDetail> detail );
Void Update (System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> entity );
}
}

 

Service implementation

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Domain;

Namespace Service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.