1. Interface interface
An interface is equivalent to an abstract class without a method implementation, and the interface method does not add various access levels: Public,private, for example.
Follower Yang Lu, wrote the following code:
public interface flyable
{
void Fly ();
}
public interface Runable
{
void run ();
}
public class Bird:flyable,runable
{
#region Flyable Members
public void Fly ()
{
Console.WriteLine ("Little Bird is Flying.");
}
#endregion
#region Runable Members
public void Run ()
{
Console.WriteLine ("Little Bird is Running.");
}
#endregion
}
2. Common system interface
Using, an object that implements the IDisposable interface can use a using to make a resource declaration, and the Dispose method is called automatically after the using scope. The difference between Dispose and close: Implementing the IDisposable interface must define the Dispose method, but there is not necessarily a close method, and many implementations of Dispose are calling the Close method. SqlConnection close can be reopened later, but Dispose will no longer be used.
The use of the most used is in the dealings with unmanaged code, such as using (SqlConnection con=new SqlConnection ()) {XXXXXXXX}, eliminating the code to release resources, concise development work.
foreach: Objects that implement the IEnumerable interface can traverse using foreach. Declare an object and look at the definition of IEnumerable, and discover that it has a property interface and two method interfaces, Current,movenext, and so on.
3. List ArrayList
ArrayList can be seen as dynamic arrays. ADD, Clear, Contains, Count, Remove, RemoveAt, ToArray (conversion, no matter), indexer
All array types int[], string[], and so on in C # are inherited from the array class.
Here's a little problem with Yang Lu, and I know this is a small topic that he went to Microsoft for an interview (as anyone who has seen Yang Lu's "My Microsoft Interview Experience") is a topic of odd and even separation: there is a string of integers separated by spaces, Write a program to rearrange the integers as follows: Odd appears on the left and even on the right. For example ' 2 7 8 3 22 9 ' displayed as ' 7 3 9 2 8 22 '.
The most common solution:
#region ArrayList odd-even separation
Int[] Nums = {3, 8, 11, 30, 55, 22};
ArrayList list = new ArrayList ();
foreach (int i in nums)
{
if (i%2==1)//Odd
{
List. ADD (i);
}
}
foreach (int j in Nums)
{
if (j%2==0)//Even
{
List. Add (j);
}
}
foreach (int i in list)
{
Console.Write (i+ ",");
}
#endregion
Of course, there are a lot of ArrayList in the ointment: The data is not known what type of data put in it; it is not possible to prevent the entry of illegal types, and returning ArrayList to other functions makes the caller very confused. The difference between the variable, the return value type, and the actual object type. Intarraylist,stringarraylist and Endless. Therefore, a generic list of lists is present.
Here, follow the boss to understand the collection, ArrayList, HashSet, Hashtable, dictionary, etc. can be called the collection class. Interfaces that implement IEnumerable (GetEnumerator ()) and ienumerable<t> can traverse using foreach.
4. Generics List/dictionary
List<t>. List<t> there is no different,<t> means that the data type placed in the list is of type T, because there are conventions for declaring types, so the parameters and return values of all the methods are deterministic types. All list<t> methods are generic, and the collection of paradigms provides more methods, Max, Min, sort, and so on.
Dictionary:key-value pair key value pair. Idioms cannot be repeated, but explanations can be repeated.
Dictionary<k,v>:(1) Add: Add, if duplicate, the error indexer mode setting value: Can be repeated settings, even if it does not exist or not, if repeated the new data overwrite the old data (2) ContainsKey, determine if there is this key
The boss here talks about a common question: what is a generic type of non-generic? For example, the non-generic type of dictionary<k,v> corresponds to the hashtable;list<t>→arraylist
As for why dictionary so fast? Because there is a dictionary in the area of the key value pairs, using a fixed algorithm (hash algorithm, very fast, can be considered time complexity O (1)) according to the key to calculate the address of the KVP, calculate key value of the key value pair should store the address, the key value pair into the specified address. You can find the data by first calculating the address of the key. Find your room number based on key, instead of looking for it in one room.
5. Other collection classes
HASHSET<T>: Cannot hold duplicate data, duplicate data only keep one copy. Add (t value) adds an element, Contains (t value) determines whether an element exists, HashSet uses an algorithm similar to dictionary, so the Contains method is very efficient and the time complexity is O (1).
Here, Lao Yang gave an interview question: The existing 1~10 a total of 10 integers, has been randomly placed into an array of 8 elements a[8]. Ask to find the 2 numbers that are not put into the array. Note: The program does not implement the natural number randomly placed array process, test data {9,8,5,3,1,10,2,7}.
Then, with the most simple algorithm-free code implementation:
#region 07. Face question
Int[] Nums = {9, 8, 5, 3, 1, 10, 2, 7};
hashset<int> set = new hashset<int> ();
foreach (int i in nums)
{
Set. ADD (i);
}
for (int i = 1; i <=; i++)
{
if (!set. Contains (i))
{
Console.WriteLine (i);
}
}
#endregion
Stack<t>, Stack, first into the back, Push (stack), Pop (out of the stack).
Queue<t>, queue, first in, first out: Enqueue (Team), Dequeue (outbound)
C # Basics and common data structures learning notes