C ා basic knowledge series - 13 introduction to common class library

Keywords: C# less Programming

0. Preface

One foreword for each article, introduce the content of this article. The previous contents are all for the introduction of some knowledge points. This article introduces some commonly used classes and namespaces in actual development. This is a series. There are about three or four episodes. Well, that's it.

1. System namespace

System space is the basic namespace of C ා, which defines common values, data types and various types of base classes. Of course, it also includes many classes used in the operation of C ා programs. For details, you can visit the official API description of Microsoft. Here is a brief introduction to some of the most commonly used classes in development.

1.1 Console

Console console class, which represents the standard input flow, output flow, and error flow of a console application. This is what Microsoft official documents give. In fact, the console class can also be used in some other types of projects. Because the console class is the interaction between the program and the terminal, when the program holds a terminal, the class can output the content correctly.

As usual, let's take a look at its declaration: public static class Console. It can be seen that this is a static class, and a clear concept is needed:

  • In C ා or even most programming languages (supporting static classes), static classes cannot be inherited, and the methods of static classes are tool methods;
  • A static class has no constructor or object;
  • Methods in static classes are static methods
  • To access a static method of a class, you need to access it through the class name. Method name

Then we can use the: Console. Method name to call the Console method.

Let's take a look at the common methods used by Console in development:

  1. Output:

    public static void Write (<T> value); //T for type
    public static void Write (string format, params object[] arg);
    

    There are 17 overloaded versions of Write method in total, of which there are more than two commonly used versions (not two versions). In the first method, T represents 10 basic data types of C, plus an Object.

    The function is to convert parameters to strings and print them to the console, so the effect is the same as converting objects to strings and then printing them. Therefore, the parameter type of the second call method is the same as that of String.Format.

    Example code:

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Print test...");
        }
    }
    

    The effect is as follows:

    As shown in the figure above, a frame with a black background will appear, and then the printed content will be displayed.

    C ා there is another way to output the console: WriteLine, which means to Write a line by name, as well as the actual performance. Each time the method outputs a new line, Write will only continue to output at the end of the last output.

    Example:

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Print test...");
            Console.Write("Write Output test");
            Console.WriteLine();
            Console.WriteLine("This line is called WriteLine");
            Console.WriteLine("This line is also called WriteLine Output");
        }
    }
    

    Operation result:

    Unlike Write, WriteLine allows no parameter calls, indicating that an empty line is output.

  2. Get user input:

    public static int Read ();
    public static string ReadLine ();
    

    Console is not so fancy in terms of reading. There are only two commonly used readings. The first is to read a character from the input stream and return - 1 if there is no input; the second is to read a line of characters from the input.

    The problem of input stream and return - 1 when the stream has no content is not covered here. There are small partners who can wait for the update of IO chapter.

    Second, it's very interesting to get a line of input content instead of a character. That is to say, when the user decides to input this line of content and clicks new line, the program can read the input result.

    Here's an example:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Read test");
            Console.WriteLine("Please enter anything and press enter:");
            var key = Console.Read();
            Console.WriteLine($"Enter:{key}");
            Console.WriteLine();
            Console.ReadLine();
            key = Console.Read();
            Console.WriteLine($"Enter:{key}");
            Console.ReadLine();
            Console.WriteLine("ReadLine test");
            Console.WriteLine("Please enter anything and wrap:");
            var line = Console.ReadLine();
            Console.WriteLine($"Enter:{line}");
            Console.WriteLine("End of sample");
    
        }
    }
    

    The results are as follows:

    In the example, I call a ReadLine before calling Read every time. This is because I input characters once in the console, then press enter and line feed. There are two inputs, so I will continue to Read the last unread content in the second Read, so I use the ReadLine feature to Read the unread content once to ensure that the next call must be from The console reads user input.

Of course, these are not the only contents of the Console class, but these are the methods we are most exposed to.

1.2 Math

The mathematical tool class in C provides constant and static methods for trigonometric, logarithmic and other general mathematical functions. This class is also a static class, of course, this will not affect our curiosity about it.

Come on, let's see what's in it.

public static T Abs (<T> value);//T for decimal, int, double, float, long, sbyte, short, return absolute value
public static double Acos (double d);//Returns the angle whose cosine value is the specified number.
public static double Acosh (double d);//Returns the angle at which the hyperbolic cosine value is a specified number.
public static double Asin (double d);// Returns the angle whose sine value is the specified number.
public static double Asinh (double d);// Returns the angle at which the hyperbolic sine is a specified number.
public static double Atan (double d);// Returns the angle whose tangent value is the specified number.
public static double Atan2 (double y, double x);// Returns the angle whose tangent value is the quotient of two specified numbers.
public static long BigMul (int a, int b);// Generates the complete product of two 32-bit numbers.
public static double BitDecrement (double x);// Returns the next minimum less than x.
public static double BitIncrement (double x);// Returns the next maximum value greater than x.
public static double Cbrt (double d);// Returns the cube root of a specified number.
public static T Ceiling (<T> d); //T represents decimal, double, and returns the minimum integer value greater than or equal to the specified number.
public static double Cos (double d);// Returns the cosine value of the specified angle.
public static int DivRem (int a, int b, out int result);// Calculates the quotient of two numbers and returns the remainder in the output parameter. result is the remainder
public static double Exp (double d);//Returns the specified power of E, e is the base of the natural logarithm
public static T Floor (<T> d); //T represents decimal, double, and returns the maximum integer value less than or equal to the specified double precision floating-point number.
public static int ILogB (double x);// Returns the base 2 logarithm of the specified number.
public static double Log (double d);//Returns the natural logarithm of the specified number (bottom e).
public static double Log (double a, double newBase);// Returns the logarithm of the specified number when the specified base is used. newBase as the bottom
public static double Log10 (double d);//Returns the base 10 logarithm of a specified number
public static double Log2 (double x);//Returns the base 2 logarithm of the specified number.
public static T Max(<T> t1,<T> t2);// T for decimal, int, double, float, long, sbyte, short, return the larger one
public static T Min(<T> t1,<T> t2);// T for decimal, int, double, float, long, sbyte, short, return the smaller of the two
public static double Pow (double x, double y);// Returns the specified power of the specified number.
public static double Round (double a);// Round the double precision floating-point value to the nearest integer value and the middle point value to the nearest even number.
public static double ScaleB (double x, int n);//Returns the x * 2^n of a valid calculation.
public static int Sign (<T> value); // T stands for decimal, double, float, int, long, sbyte, short, and returns the integer indicating the number symbol.
public static double Sin (double a);// Returns the sine of the specified angle.
public static double Sinh (double value);//Returns the hyperbolic sine of the specified angle.
public static double Sqrt (double d);//Returns the square root of the specified number.
public static double Tan (double a);//Returns the tangent value of the specified angle.
public static double Tanh (double value);//Returns the hyperbolic tangent of the specified angle.
public static T Truncate (<T> d);//T stands for decimal, double, and calculates the integer part of a number.

Well, there are a lot of methods, but only the following are worth noting:

public static T Ceiling (<T> d); //T represents decimal, double, and returns the minimum integer value greater than or equal to the specified number.
public static T Floor (<T> d); //T represents decimal, double, and returns the maximum integer value less than or equal to the specified double precision floating-point number.
public static T Truncate (<T> d);//T stands for decimal, double, and calculates the integer part of a number.

Although the results of these three methods are integers, the return type is not integers, so we need to perform a type conversion when using them. The Math class also has two noteworthy fields:

public const double E = 2.7182818284590451;// Represents the base of the natural logarithm, specified by the constant e.
public const double PI = 3.1415926535897931;// Represents the ratio of the circumference of a circle to its diameter, specified by the constant π.

These two are also the only two fields in Math. These are two famous irrational numbers in Math. Only a part of valid values are intercepted here.

1.3 Random

Random in C ා represents a pseudo-random number generator, which is an algorithm that can generate a number sequence satisfying some random statistical requirements. Here, I'll explain the use of random. I'll study the specific principle later.

Random is a class, so different from the previous two classes is that using random to generate random numbers requires constructing a random object in advance. Random's common methods are as follows:

public virtual int Next ();// Returns the random number of an integer
public virtual int Next (int maxValue);//Returns a nonnegative random integer less than the specified maximum
public virtual int Next (int minValue, int maxValue);//Returns any integer in the specified range.
public virtual double NextDouble ();//Returns a random floating-point number greater than or equal to 0.0 and less than 1.0.

Let's first demonstrate the basic application of Random:

class Program
{
    static void Main(string[] args)
    {
        Random rand = new Random();
        for(int i = 0;i< 10; i++)
        {
            Console.WriteLine($"The first{i + 1}Secondary generation:{rand.Next()}");
        }
        Console.ReadLine();
    }
}

Print results:

It can be seen that the result numbers returned directly from Next are relatively large, so when using it, Next (int minValue, int maxValue) is generally used to limit the return value.

Back to the beginning, Random is a class. Each time it is initialized, the system will automatically calculate a seed for it. If you build Random objects repeatedly, you may generate a repeated sequence, that is, the result of each call is the same. (of course, officially, I didn't measure it in the net core 3.1 environment.)

class Program
{
    static void Main(string[] args)
    {
        for(int i = 0;i< 5; i++)
        {
            Random rand = new Random();
            for(int j = 0; j < 10; j++)
            {
                Console.WriteLine($"The first{i}individual Random The first{j}Secondary generation:{rand.Next()}");
            }
        }
        Console.ReadLine();
    }
}

This is the test code. Interested partners can try it on their own.

Please pay attention to more My blog

Posted by danger2oo6 on Fri, 24 Apr 2020 11:05:32 -0700