C ා basic knowledge series – 13 introduction to common class library (1)

Time:2020-5-27

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 statement: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 toClass name. Method nameTo visit

Then we can use:Console. Method nameTo call the console’s methods.

Let’s take a look at the common methods used by console in development:

  1. Output:

    Public static void write (< T > value); // T represents 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 print parameters into strings to the console, so the effect is the same as that of converting objects into strings first and then printing. Therefore, the parameter type of the second call method is the same asString.FormatIs consistent.

    Example code:

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

    The effect is as follows:

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

    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:WriteLineFrom the point of view of name, it means to write one line, so does the actual performance. Each time the method outputs a new line of content, andWriteOutput will only continue 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 a call to writeline");
            Console.WriteLine ("this line is also output by calling writeline");
        }
    }

    Operation result:

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

    AndWriteThe difference is,WriteLineNo parameter call is allowed, 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 any content and press enter:");
            var key = Console.Read();
            Console.WriteLine ($"entered is: {key}");
            Console.WriteLine();
            Console.ReadLine();
            key = Console.Read();
            Console.WriteLine ($"entered is: {key}");
            Console.ReadLine();
            Console.WriteLine ("readLine test");
            Console.WriteLine ("please enter anything and wrap:");
            var line = Console.ReadLine();
            Console.WriteLine ($"entered: {line}");
            Console.WriteLine ("end of sample");
    
        }
    }

    The results are as follows:

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

    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, flush, 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 whose hyperbolic cosine value is the 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 hyperbolic sine value as the angle of the specified number.
Public static double atan (double D); // returns the tangent value as the angle of 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 bitdecision (double x); // returns the next minimum value 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 the specified number.
Public static t ceiling (< T > d); // T represents decimal and 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 natural logarithm
Public static T floor (< T > d); // T represents decimal and 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 logarithm of an integer with a base of 2 for 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 using the specified base. Newbase as the bottom
Public static double log10 (double D); // returns the base 10 logarithm of the 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 stands for decimal, int, double, float, long, sbyte, short, and returns the larger one
Public static t min (< T > T1, < T > T2); // T stands for decimal, int, double, float, long, sbyte, short, and returns the smaller one
Public static double pow (double x, double y); // returns the specified power of the specified number.
Public static double round (double a); // rounds a 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 valid calculation.
Public static int sign (< T > value); // T stands for decimal, double, flush, int, long, sbyte, short, and returns the integer indicating the number sign.
Public static double sin (double a); // returns the sine value of the specified angle.
Public static double Sinh (double value); // returns the hyperbolic sine value 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 represents decimal and 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 and double, and returns the minimum integer value greater than or equal to the specified number.
Public static T floor (< T > d); // T represents decimal and 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 represents decimal and 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; // indicates the base of natural logarithm, which is specified by the constant E.
Public const double pi = 3.1415926535897931; // 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 an integer random number
Public virtual int next (int maxvalue); // returns a non negative random integer less than the specified maximum value
Public virtual int next (int minValue, int maxvalue); // returns any integer within 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 ($"build {I + 1}:{ rand.Next ()}");
        }
        Console.ReadLine();
    }
}

Print results:

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

It can be seen that the result numbers returned by the direct next are relatively large, so when using it, you will generally use theNext (int minValue, int maxValue)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 {I} random generated at {J}:{ rand.Next ()}");
            }
        }
        Console.ReadLine();
    }
}

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

Please pay more attention to my blog “Mr. Gao’s cabin”

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