Review of C# core knowledge – methods, String, StringBuilder in 4.object

Methods in 1.object

Static method in object:

The static method Equals judges whether two objects are equal:
The final judgment is handed over to the Equals method of the left object,
Regardless of the value type, the reference type will be compared according to the rules of the Equals method of the left object

Static method Reference Equals:
Comparing whether two objects are the same reference is mainly used to compare objects of reference type.
Value type objects always return false.

//value type Equals method comparison
        Debug. Log(object. Equals(1,1));
        //Reference type Equals method comparison
        Test t = new Test();
        Test t2 = new Test();
        //The reference type compares whether the corresponding paths are consistent
        Debug. Log(object. Equals(t, t2));

        //ReferenceEquals is only used to compare reference type objects.
        Debug.Log(object.ReferenceEquals(t, t2));

Member method in object:

Ordinary method GetType:
This method is very important in reflection-related knowledge points. After that, we will explain the Type type returned here in detail.
The main function of this method is to obtain the type Type of the object at runtime.
You can do a lot of operations on objects through Type combined with reflection-related knowledge points.

Common method Memberwiseclone:
This method is used to obtain a shallow copy object of the object. The colloquial meaning is that a new object will be returned.
But the reference variable in the new object will be the same as in the old object.

class Test {
    public int i = 0;
    public Test2 t2 = new Test2();
    //Pass out its own MemberwiseClone method
    public Test Clon()
    {
        return MemberwiseClone() as Test;
    }
}
class Test2
{
    public int i = 0;
}
public class GetInfo : MonoBehaviour
{
    private void Start()
    {
        Test t = new Test();
        Test test = t.Clon();
        Debug. Log("t.i " + t.i);
        Debug.Log("t.t2.i " + t.t2.i);
        Debug.Log("test.i " + test.i);
        Debug.Log("test.t2.i " + test.t2.i);
        Debug. Log("--------------------");
        test.i = 10;
        test.t2.i = 10;
        Debug. Log("t.i " + t.i);
        Debug.Log("t.t2.i " + t.t2.i);
        Debug.Log("test.i " + test.i);
        Debug.Log("test.t2.i " + test.t2.i);
    }
}

Virtual method in object:
virtual method Equals
The default implementation still compares whether the two are the same reference, which is equivalent to Reference Equals. However, Microsoft has rewritten this method in the base class system.Value Type of all value types to compare values for equality. We can also override this method to define our own rules for comparing equality

virtual method GetHashCode
This method is to obtain the hash code of the object (a unique code that is calculated by an algorithm and represents the object. The hash code of different objects may be the same, and the specific value is determined according to the hash algorithm). We can rewrite this function by ourselves Define the hash code algorithm of the object. Under normal circumstances, we use very little and basically don’t use it.

Virtual method Tostring (very common)
This method is used to return the string represented by the current object. We can override it to define our own object-to-string rules. This method is very commonly used. When we call the print method, the default is to use the object’s To string method to print out the content.

2. String

Acquisition at the specified position of the string:

//The essence of the string is a char array
        string str = "123";
        Debug. Log(str[0]);//1

String concatenation:

 string str = "123";
        str = string. Format("{0}{1}", str, 123);
        Debug. Log(str);//123123

Forward lookup character position:

string str = "Wahaha construction team";
        int index = str.IndexOf("ha");
        int li = str.IndexOf("in");
        Debug. Log(index);//1
        Debug. Log(li);//-1

If found, return the index of the first match, if not found, return -1.

Reverse lookup character position:

string str = "Wahaha construction team";
        int index = str.LastIndexOf("ha");
        Debug. Log(index);//2

There are multiple target characters in the character, and if you want to get the following character, you can use LastIndexOf.

Remove the specified position and following characters:

 string str = "Wahaha Construction Team";
        //Direct remove will not delete the original character, only return a new character
        //str. Remove(3);
        str = str. Remove(4);
        Debug.Log(str);//Wahaha Shi
        //Delete the specified character at the specified position
        str = str.Remove(2,1);//Index 2 starts to delete a character
        Debug.Log(str);//Wahashi

Replace specified characters:

string str = "Wahaha construction team";
        str = str.Replace("Ha","Hey");
        Debug.Log(str);//Hey Hey construction team

Case Conversion:

string str = "sfsdfs";
        str = str.ToUpper();
        Debug.Log(str);//SFSDFS

        str = str.ToLower();
        Debug.Log(str);//sfsdfs

String interception:

string str = "Wahaha construction team";
        //Intercept the characters after the specified position
        str = str. Substring(2);
        Debug.Log(str);//Ha construction team
        str = str. Substring(1,1);
        Debug.Log(str);//Apply

String cut:

 string str = "Wahaha. Construction Team";
        //Intercept the characters after the specified position
        string[] ss = str. Split('.');
        Debug.Log(ss[0]);//Wahaha

 string[] str = { "h", "e", "l", "l", "o" };
        str = str.Reverse().ToArray();
        for (int i = 0; i < str. Length; i ++ )
        {
            Debug. Log(str[i]);
        }
3. StringBuilder

A public class provided by C# for processing strings
The main problems to be solved are:
1. Modify the string without creating a new object. It can be used for strings that require frequent modification and splicing, which can improve performance
2. The namespace needs to be referenced before use

String Builder has a capacity problem, every time it is added to it, it will automatically expand the capacity

StringBuilder stringBuilder = new StringBuilder("1111");
        // get capacity
        Debug.Log(stringBuilder.Capacity);//16 The default initialization gives 16 lengths
        //Get character length
        Debug.Log(stringBuilder.Length);//real length

Add, delete, modify and check:

 StringBuilder str = new StringBuilder("1111");
        //increase
        str.Append("222");
        str.AppendFormat("{0}{1}", 15551, 254454452);
        Debug. Log(str);
        Debug.Log(str.Capacity);//Exceeding the first limited length, the length will be re-specified and doubled to 32
        //insert
        str. Insert(1, "999");
        Debug. Log(str);//1999...
        //delete
        str.Remove(0, 1);//Delete 1 element from index 0
        //clear
        //str. Clear();
        //check
        Debug. Log(str[2]);
        //change
        str[0] = 'A';
        //replace
        str.Replace("9", "B");
        Debug. Log(str);
        //judgment is equal
        if (str. Equals("123"))
        {
            Debug. Log("1");
        }