C # 1- variables and expressions

Basis of the description of the computer program: a series of operations for processing data. That shows the results of the initial state is the presence of data in computer memory 0 and 1 data stream.

Operate on the data, the need to store data in some way deal with them in certain ways, these two methods provided by the variables and expressions .

First, the basic grammar

1, the basic structure

// Code Outline Function: #region and #endregion keyword to define the beginning and end of the expandable and retractable, where # is actually any keyword beginning of a preprocessor directive 
#region 
// use using namespace access, external references name namespace, you need to write the namespace qualified name of 
a using System;
 a using System.Collections.Generic;
 a using System.Ling;
 a using System.Text;
 a using System.Threading.Tasks;
 // a using static keyword, the key word allows static member directly in C # program scope of 
the using  static the System.Console;
 #endregion 

# namespace display definition namespace
 namespace Project0_01 
{ 
    // definition of a class 
    class program 
    { 
        //Defined function, this is the main function, a program has only one main function, as used herein, static members 
        static  void the Main ( String [] args) 
        { 
            // Here placement method, i.e. code block 
            Console.WriteLine ( " the Hello, World! " ); 
            Console.ReadKey (); 
            / * after a keyword cited above, can be written as: 
             the WriteLine (" the Hello, World ");! 
             ReadKey (); 
            * / 
        } 
    } 
}

 

2, the identifier

  Identifier is a string, such as for naming the variables, methods, and many other parameters of the program structure.

  1) naming rules:

    * Composition: uppercase and lowercase letters, underscore, @ characters, numbers (do not be the first character);

    * @ Character can only be in the first place;

    * Do not use the keyword;

  2) naming convention:

    * Camel nomenclature: the first letter of the first word lowercase, and the rest of the word capitalized; (variable)

    * Pascal naming convention: the first letter of the first word is capitalized, the use of the abbreviation of the word, are all uppercase characters; (method, class)

3, keyword : C # to define token strings;

4, Console.Write () and Console.WriteLine () method - the output text in the program, and accepts keyboard input.

  1. BCL Console a class, the method comprising input and output to the console window in the System namespace.

    Write () and the WriteLine () difference: WriteLine () output at the end of a multi-line break.

  

  2. The format string: Console.WriteLine (containing replacement marking string format, replacing the value 1, 2 ...... substitute value);

    NOTE: Alternatively replacement value be less than the number of markers

    When used in conjunction with the string and the output statement, the output requires some special content such as the file path, requires not escape character recognition, can be added in front of the @ character string unescape represented;

 

  3. Variables

    Format: data type variable name;

       Data type variable name = value;

string name="John"int age=18;
Console.WriteLine("Hello!I'm {0},I'm {1}",name,age);
Console.ReadKey();

 

5, data related presentations

  1. Simple types

    . A Type Integer: int

    . B float type (can be considered as a decimal, the default type double, float type need to use add f / F values ​​after): float, double

    c characters:. char, string: string, Boolean Type: bool (only two return values ​​true and false)

      char is a single character, string is a collection of characters

  2. literals: text and numeric values ​​represent;

   Escape character: a character has special functions;

   Action unicode value: is a hexadecimal number indicating the number of characters stored in memory in which may be used to represent an escape character;

 

6, mathematical operators: add, subtract, multiply, divide, modulo

  1. With regard to the result of the mathematical operation types:

    . A obtained when the same type on both sides of the operand, and the result returned as the type of operand;

       . B When both sides are inconsistent operand types, the returned results and a large number of types of operations are coordinated;

 

  2. String

    . A string concatenation, for connecting two string, returns a string;

    . B string and a number added to a string of digital first, and then joined, the result is a string;

 

  3. Since the addition and decrement operators +, -

    If self plus or decrement operators in front of the operand, will first increment / decrement, and then performs the remainder operation;

    Later, the remaining operation is executed first, the last increment / decrement;

1  // Exercise: accepting two numbers entered by the user, and calculates and outputs 
2 Console.WriteLine ( " Please enter the first number: " );
 3  // ReadLine () is one of Console static class members, get keyboard input
 . 4  // TonInt32 () convert static class is one of the members, a data type conversion int 
. 5  int num1 = Convert.ToInt32 (Console.ReadLine ());
 . 6 Console.WriteLine ( " Please enter the second number: " );
 . 7  int num2 = Convert.ToInt32 (Console.ReadLine ());
 . 8  int sUM = + num1 num2;
 . 9 Console.WriteLine ( " two numbers is: " + sUM);

 

    

  

 

 

  

        

    

 

  

 

Guess you like

Origin www.cnblogs.com/Free-Ink/p/12515177.html