C # language Basics

Source: Internet
Author: User

I. INPUT and OUTPUT statements

The main input and output statements of the main function are:

Console.Write (); Output statement, do not wrap line

Console.WriteLine (); Output statement, wrap line

Console.read (); Input statement, does not wrap line

Console.ReadLine (); Input statement, wrap line

C # statements are sensitive to the case of English letters, and you must pay attention to the case of letters when writing code. After writing each sentence of the code, you must add ";" to indicate the end of the sentence. Also, when writing code, all punctuation must be entered in English input state, otherwise, the input code will be error. When you enter the program, you can Press "alt+→", pop-up program statement prompts, to help us complete the writing of the statement. The "+" in the input and output statements is not a representation of the addition operation, but rather a concatenation of the contents before and after "+".

Note There are several ways to do this:

Comment Line

/* * * comment on an area

Put it on the function and annotate the function.

You can also annotate a selected piece of text using the comment Select Line button or its shortcut key on the toolbar in the Visual Studio software. Ctrl+e,c.

In this part of the study, I did a little exercise:

get to the user's name, age and work unit, and complete the "I call * * *, this year * * * years old, work in * * *. "In a word.

Console.WriteLine ("Please enter your name:");

String a = Console.ReadLine ();

Console.WriteLine ("Please enter your Age:");

String B = Console.ReadLine ();

Console.WriteLine ("Please enter your work unit:");

String c = Console.ReadLine ();

Console.WriteLine ("I Call" +a+ ", this year" +b+ "years old, in the" +c+ "work. ");

Console.WriteLine ("I call {0}, this year {1} years old, working at {2}.") ", a,b,c);//use a placeholder to take up the empty space first

Console.ReadLine ();

Ii. Types of data

C # language . NET type (Common language) Size (bytes) Value Range

Basic data types

(value type)

Integral type Byte Byte 1 0-255
Short Int16 2
Int Int32 4 -231~231
Long Int64 8
Floating point Type Float Single 4
Double Double 8
Decimal Decimal 16
Character type Char Char 2 Any character
Boolean type bool Boolean 1 True or False
Date Time Datatime
Enum type Enum
Structure type struct
Reference class String type String String Character sequences

There are three main ways to perform basic type conversions:

1. Use ()

2. Use Convert

3. Using the Parse

Example:

int A;
float B = 3.14;
A = (int) b; With ()
A = Convert.ToInt32 (b); With convert
a = Int. Parse (B.tostring ()); The value to be placed with parse is a string type

In this part of the study, I do the small exercise is:

Q: What time is the old Wolf Wolf?

If the morning, the direct output time and add am, if the afternoon, the actual time minus 12 plus PM (12-hour clock to represent the time)

Console.Write ("What time is the wolf old Wolf?" ");

int time = Int.   Parse (Console.ReadLine ()); ReadLine command default data type is string, cast to int type with parse

String ap = time > 12? (time-12) + "PM": time+ "AM";

Console.WriteLine (AP);

Console.ReadLine ();

Third, operator

Classification Symbol Explain Priority level
Count ++  -- Gaga minus minus

From high to low, that is, the execution order is from top to bottom.

(highest precedence of parentheses)

* / % Multiplication to take the remainder
+  - Add and Subtract
Relationship > < >= <= Greater than or equal to less than or equal to
= =! = equals Not equal to
&& With (and)
|| Or
Non ( Note: Priority at the top level of this table )
Conditional operators ? : The only ternary operator if
Assign value =  +=  -=  *=  /=  %= such as: X-=4. That is x=x-4

In an assignment operation, the first + + is performed +1 and then assigned, and the next + + executes the assignment before performing the +1 operation.

When a new value of two data classes is calculated, the type with the largest range is automatically taken.

In this section of the study, I completed the small exercise is:

determine if an integer within 100 is a multiple of 7, or does it contain 7?

Console.Write ("Please enter an integer within 100:");
int num = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Is it a multiple of 7?") "+ (num%7==0));
Console.WriteLine ("is the single digit 7?") "+ (num%10==7));
Console.WriteLine ("is the 10-digit number 7?") "+ (num/10==7));
Console.ReadLine ();

C # language Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.