asp.net MVC bedroom management system (source code + database)

ASP.NET project bedroom management system source code for everyone to learn and communicate

File: n459.com/file/25127180-478966639

The following are irrelevant:

-------------------------------------------Dividing line----- ----------------------------------------

Having been in the industry for many years, I have not systematically sorted out this pair of Li Kui and Li Gui.

Recently, I was stunned by Why God's "Let's Actually, LRU is the same." Scheme 1 uses an array to implement LUR. The handwritten algorithm involves this pair of interfaces. I take this opportunity to cover this pair of difficult enemies.

IEnumerator
IEnumerator and IEnumerable interfaces have similar names. These two interfaces are usually used together, and they have different purposes.

The IEnumerator interface provides an iterative function for the collection inside the class. IEnumerator requires you to implement three methods:

MoveNext method: This method adds 1 to the collection index and returns a bool value indicating whether the end of the collection has been reached.
Reset method: It resets the collection index to its initial value -1, which invalidates the enumerator.
Current method: Returns the current object
IEnumerable at the position. The
IEnumerable interface provides support for foreach iteration. IEnumerable requires you to implement the GetEnumerator method.

public IEnumerator GetEnumerator()
{ return (IEnumerator)this; } Which interface should I use? Based on the above rhetoric alone, it is difficult to distinguish the usage scenarios of the two interfaces.



The IEnumerator interface provides iteration over collection type objects in a class,

The IEnumerable interface allows enumeration using a foreach loop.

However, the GetEnumerator method of the IEnumerable interface returns an IEnumerator interface. To implement IEnumerable, you must also implement IEnumerator. If you do not implement IEnumerator, you cannot convert the return value of the GetEnumerator method of IEnumerable to the IEnumerator interface.

From the English root:
IEnumerator interface represents an enumerator, which defines the enumeration method;
IEnumerable interface represents that the object has the property of being enumerated.

In short, the use of IEnumerable requires the class to implement IEnumerator. If you want to provide support for foreach, then implement these two interfaces.

Best practice
Implement IEnumerator in nested classes so that you can create multiple enumerators.
Provide exception handling for the Current method of IEnumerator.
Why do you want to do this?
If the content of the collection changes, the reset method will be called, and immediately after the current enumerator is invalid, you will receive an IndexOutOfRangeException (other circumstances may also cause this exception). So execute a Try...Catch block to catch this exception and throw an InvalidOperationException exception, prompting that the collection content is not allowed to be modified during iteration.
This is why we often report InvalidOperationException when trying to modify the iteration object in foreach.

Take the car list as an example to implement the IEnumerator IEnumerable interface

using System;
using System.Collections;
namespace ConsoleEnum
{
public class cars : IEnumerable
{
private car[] carlist;

    //Create internal array in constructor.
    public cars()
    {
        carlist= new car[6]
        {
            new car("Ford",1992),
            new car("Fiat",1988),
            new car("Buick",1932),
            new car("Ford",1932),
            new car("Dodge",1999),
            new car("Honda",1977)
        };
    }
    //private enumerator class
    private class  MyEnumerator:IEnumerator
    {
        public car[] carlist;
        int position = -1;

        //constructor
        public MyEnumerator(car[] list)
        {
            carlist=list;
        }
        private IEnumerator getEnumerator()
        {
            return (IEnumerator)this;
        }
        //IEnumerator
        public bool MoveNext()
        {
            position++;
            return (position < carlist.Length);
        }
        //IEnumerator
        public void Reset()
        {
            position = -1;
        }
        //IEnumerator
        public object Current
        {
            get
            {
                try
                {
                    return carlist[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }  //end nested class
  public IEnumerator GetEnumerator()
  {
      return new MyEnumerator(carlist);
  }
}

}
When foreach cars, you can clearly see

Foreach syntactic sugar The first time you contact cars, you will actually enter the GetEnumerator() method implemented by cars.
Each iteration of foreach will actually enter the get accessor of the Current property.

Guess you like

Origin blog.csdn.net/gumenghua_com1/article/details/112560525