The collection class Contains method in-depth explanation and interface example

Source: Internet
Author: User

. Net equality: An in-depth explanation of the collection class Contains method

Http://www.cnblogs.com/ldp615/archive/2009/09/05/1560791.html

1, the concept of interface and statement
An interface is a protocol used to define a program that describes a set of related behaviors that can belong to any class or struct. Interfaces can have methods, properties, events, and indexers, or any combination of these four members, but cannot contain fields.
So what are the features of the interface?
• An interface is similar to an abstract base class: Any non-abstract type that inherits an interface must implement all members of the interface (note: If Class A inherits interface B, then a must implement the properties, methods, etc. defined in B).
• Interfaces cannot be instantiated directly
• Interfaces can contain events, indexers, methods, and properties
• The interface does not contain the implementation of the method
• Classes and interfaces can inherit from multiple interfaces
• The interface itself can inherit multiple interfaces
In addition to the interface and interface names that are required when declaring an interface, all other options are optional. Interfaces can also be implemented using modifiers such as new, public, protected, intenal, and private, but interface members must be common.
2, the implementation and inheritance of the interface
When declaring a class that implements an interface, you need to include the name of the interface that the class implements in the list of base classes.
Here is a small example to explore:
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace _
{
Interface IMyInterface
{
<summary>
Number (readable and writable)
</summary>
String ID
{
Get
Set
}
<summary>
Name (Readable and writable)
</summary>
String Name
{
Get
Set
}
<summary>
Show the defined number and name
</summary>
void Showinfo ();
}
Class program:imyinterface//inherit from interface
{
String id = "";
String name = "";
<summary>
Number
</summary>
public string ID
{
Get
{
return ID;
}
Set
{
id = value;
}
}
<summary>
Name
</summary>
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}
<summary>
Show the defined number and name
</summary>
public void Showinfo ()
{
Console.WriteLine ("Number \ t name");
Console.WriteLine (ID + "\ T" + Name);
}
static void Main (string[] args)
{
Program program = new Program (); Instantiating a program class object
IMyInterface imyinterface = program; Instantiating an interface using a derived class object IMyInterface
Imyinterface.id = "TM"; Assigning a value to an ID property in a derived class
IMyInterface. Name = "C # 2.0 from Getting Started to application development"; Assigning a value to the Name property in a derived class
IMyInterface. Showinfo (); Calling a method in a derived class to display a defined property value
}
}
}
The above example is a simple class that inherits an interface, such as to implement a corresponding member of an interface class that must be public, non-static, and have the same name and signature as the interface member. And the instantiation of the interface cannot be like a class program = new Programs (), where the red part of the above procedure is the instantiation of the interface (using the derived class object to instantiate the interface).
Single inheritance when this, then multiple inheritance? OK, let's take a look at the next example:
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace _
{
Interface Ipeople
{
<summary>
Name
</summary>
String Name
{
Get
Set
}
<summary>
Gender
</summary>
String Sex
{
Get
Set
}
}
Interface Iteacher:ipeople//Inherit public interface
{
<summary>
Teaching methods
</summary>
void teach ();
}
Interface Istudent:ipeople//Inherit public interface
{
<summary>
Learning methods
</summary>
void study ();
}
Class program:ipeople,iteacher,istudent//multi-Interface inheritance
{
String name = "";
string sex = "";
<summary>
Name
</summary>
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}
<summary>
Gender
</summary>
public string Sex
{
Get
{
return sex;
}
Set
{
sex = value;
}
}
<summary>
Teaching methods
</summary>
public void Teach ()
{
Console.WriteLine (Name + "+ Sex +" teacher ");
}
<summary>
Learning methods
</summary>
public void Study ()
{
Console.WriteLine (Name + "" + Sex + "student");
}
static void Main (string[] args)
{
Program program = new Program (); Instantiating a Class object
Iteacher iteacher = program; Instantiating an interface using a derived class object Iteacher
Iteacher. Name = "TM";
Iteacher. Sex = "male";
Iteacher.teach ();
istudent istudent = program; Instantiating an interface using a derived class object istudent
Istudent. Name = "C #";
Istudent. Sex = "male";
Istudent.study ();
}
}
}
As described in the multiple inheritance above, all methods in the inherited interface must be implemented in a derived class. OK, the single inheritance and multiple inheritance all have the understanding, is there no other need to understand? Imagine that if you inherit from interfaces B and C in a Class A, and include members with the same signature in B and C, implementing that member in a class will cause two interfaces to use that member as their implementation, however, if two interface members implement different functions, Then it will result in an interface member implementation is not correct or the two interface member implementation is not correct, this time how should we deal with it? We can display a member of the implementation interface that creates a class member that is called only through an interface and is specific to that interface.
The following is also an example to illustrate
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace _
{
Interface ImyInterface1
{
<summary>
Summation method
</summary>
<returns> The sum of the addition operations </returns>
int Add ();
}
Interface ImyInterface2
{
<summary>
Summation method
</summary>
<returns> The sum of the addition operations </returns>
int Add ();
}
Class Myclass:imyinterface1, ImyInterface2//Inherit interface
{
<summary>
Summation method
</summary>
<returns> The sum of the addition operations </returns>
int Imyinterface1.add ()//explicit interface member implementation
{
int x = 3;
int y = 5;
return x + y;
}
<summary>
Summation method
</summary>
<returns> The sum of the addition operations </returns>
int Imyinterface2.add ()//explicit interface member implementation
{
int x = 3;
int y = 5;
int z = 7;
return x + y + z;
}
}
Class Program
{
static void Main (string[] args)
{
MyClass MyClass = new MyClass (); Instantiating an interface object that inherits a class
ImyInterface1 Imyinterface1 = MyClass; Object instantiation interface using interface inheritance class
Console.WriteLine (Imyinterface1. ADD ()); Invoking a method in an interface using an interface object
ImyInterface2 Imyinterface2 = MyClass; Object instantiation interface using interface inheritance class
Console.WriteLine (Imyinterface2. ADD ()); Invoking a method in an interface using an interface object
}
}
}
In the above example, in the MyClass class, the Add method in two interfaces is implemented by two methods of displaying the interface members, and the corresponding method is used to implement the output result after instantiating different interfaces.

The collection class Contains method in-depth explanation and interface example

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.