Understanding of C\

1, The extension method must meet the following requirements
1. the class of the method must be static. Static classes cannot be instantiated
2. the method must also be static
3. the first parameter of the method must be the type you want to extend. For example, if you want to extend a method for int, the first parameter must be int.
4. there should also be a keyword this before the first parameter.
2, Summary of extension methods:
1. You can add new methods to a class without using inheritance to create a new class or modifying the original class;
2. If the extension method has the same signature as the method in the class, the extension method will not be called, that is, the extension method will be overwritten by the method with the same name in the extension class, so we need to bear the risk of being overwritten at any time when implementing the extension method (for example, if ToString() in a string class is extended, the extension method is invalid);
3. Extension methods cannot access private members of an extended class
4. Extension methods can only be called by instances, not by class names like ordinary static methods;
5. Extension methods can be used only after the namespace of the extension method is introduced.

  public static class StringFormat
  {
   public static string Test(this string str)
        {
            return "hello";
        }
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        } 
        public static bool IsMatch(this string str,string pattern)
        {
            if (str.IsNullOrEmpty()) return false;
            return Regex.IsMatch(str, pattern);
        }
        public static string Match(this string str,string pattern)
        {
            if (str.IsNullOrEmpty()) return "";
            return Regex.Match(str, pattern).Value;
        }
  }
//-----------------------------------------------
 	public class Person
    {
        public void ShowName(string name)
        {
            Console.WriteLine(name);
        }
    }
    public static class ExtendPerson
    {
        public static void ShowAge(this Person person,int age)
        {
            Console.WriteLine(age);
        }
        public static double ToDouble(this string str)
        {
            return double.Parse(str);
        }
        public static double ToFloat(this string str)
        {
            return float.Parse(str);
        }

main.cs

 class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.ShowAge(10);
            string str = "123.123";
            Console.WriteLine("toDouble-------- " + str.ToDouble());
            Console.WriteLine(str.Test());
            Console.WriteLine("".IsNullOrEmpty());

            bool b = "123456".IsMatch(@"\d+");
            string s = "123ldp615".Match("[a-zA-Z]+");
            Console.WriteLine("b "+b);
            Console.WriteLine("s "+s);

            string value1 = "hello";
            string value2 = "world";
            string vv = $"say {value1} to {value2}.";
            Console.WriteLine(vv);
        }
    }

The code mentions the ten high-level usages of C\.

tips: there is a tool class similar to Utils in the project, which has multiple helpers, such as StringHelper, XmlHelper, etc. each Helper has multiple static general methods, and then it is StringHelper Getnotnullstr ("aa"); So. Another common usage is to use new as an object to call non static methods in a class. All can be changed to extension method

tips: use a data class, n,f,str, to store and access: objects str object f object n

Static class

Class can be declared static to indicate that it contains only static members. You cannot use the new keyword to create an instance of a static class. For example, it is a common requirement to create a set of methods that do not manipulate instance data and are not associated with specific objects in your code. You should use static classes to contain those methods.
The main functions of static classes are as follows:
They contain only static members---- Function members and variables must be decorated with static
They cannot be instantiated.
They are sealed----------- Compiler automatically generates sealed tags at compile time
They cannot contain instance constructors.
Therefore, creating a static class is much the same as creating a class that contains only static members and private constructors. Private constructors prevent classes from being instantiated.
The advantage of using static classes is that the compiler can perform checks to ensure that instance members are not accidentally added. The compiler will guarantee that it will not create such benefits.
Static classes are sealed and therefore not inheritable. A static class cannot contain a constructor, but you can still declare a static constructor to assign an initial value or set a static state.

Tags: C#

Posted by jonnypixel on Mon, 30 May 2022 17:41:15 +0530