C × basis: data type

1. The first C program

using System;


namespace base_01
{
    class Program
    {
        #region code collapse block
        static void Main(string[] args)
        {
            //console output 
            Console.WriteLine("Hello World!");
            Console.Write("C#Microsoft programming language "; / / no line feed output

            //Console.ReadKey(); / / the console waits. This is not required for later versions of VS2017
        }
        #endregion
    }
}

Code Description:

(1) Each program can only have one Main method;

(2) Code block folding can be realized by region and endregion;

(3) C ා needs to reference a namespace, similar to adding a header file in C + +, and there are related classes in the namespace. For example, Console is a class in the System namespace;

(4) console output

Console.WriteLine("Hello World!");    //Wrap after output
Console.Write("C#Microsoft programming language "; / / no line feed output

Console.ReadKey();  //Console waiting, VS2019 later version does not need this sentence

2. Notes

3 slashes to annotate the method.

2 diagonal general notes

3. Variable naming rules

4. Data type

(1) Integer type

(2) Decimal type

Code example

double m1 = 12.34;
float m2 = 12.345f;   //The default decimal double type. If it's float, it's better to add an f
decimal m3 = 0.12334343m;   //Higher precision, generally indicating the variable related to money, with m added at the end

(3) Non numeric type

Sample code

char mychar = 'A';
string str = "china asd";
Console.WriteLine("first{0},the second{1}", mychar, str);

Question: the difference between string and string

C ා is case sensitive, but I found that there are strings and strings in C ා at the same time, so I was confused, so I searched online, so I learned some little knowledge.

Description of string in MSDN: string is an alias for string in the. Net Framework. String is the alias of string. String is the class in c ා and string is the class of Framework. c ා string is mapped to the string of Framework. If you use string, the compiler will compile it into string, so if you use string directly, you can make the compiler do a little less work.

If C is used, it is recommended to use string, which conforms to the specification. String always represents System.String(1.x) or:: System.String(2.0). String only represents System.String when using System; is preceded and there is no type named string (class, struct, delegate, enum) in the current namespace.

String is a keyword. String is not. That is to say, string cannot be used as the name of a class, structure, enumeration, field, variable, method or property. String can.

5. const

Variables decorated with const cannot be changed after assignment, similar to const in C + +

6. Data type conversion

            //int to string
            int value1 = 100;
            string str1 = Convert.ToString(value1);
            Console.WriteLine("str = " + str1);

            //string to int
            string str2 = "123";
            int value2 = int.Parse(str2);
            Console.WriteLine("value2 = " + value2);

7. Array

using System;

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            //Four declared usages of arrays

            //Mode 1
            int[] array1 = new int[10];
            for(int i = 0; i < array1.Length; i++)
            {
                array1[i] = i*i;
                Console.Write(array1[i] + " ");
            }

            Console.WriteLine();
            foreach(var item in array1)
            {
                Console.Write(item + " ");
            }

            //Mode 2,3,4
            int[] array2 = { 1, 2, 3, 4, 5 };
            int[] array3 = new int[3] { 1, 2, 3 };
            int[] array4 = new int[] { 1, 2, 3 };

            Console.ReadKey();
        }
    }
}

Tags: Programming less

Posted on Sun, 03 May 2020 16:12:38 -0400 by BeanoEFC