Basic C # Basics input and output functions
- Simple program Output HelloWorld
using System;//引入System库 namespace MainClass{class MainClass{ public static void Main(string[] args) { Console.WriteLine("hello world");//Console类的输出方法
public static void Main(string[] args) { string info= Console.ReadLine(); Console.WriteLine(info); }
Character conversions
- Implicit conversions: is a type of security conversion that does not result in data loss, does not require any special syntax, and is automatically implicitly converted from small to large.
int age=10;double sum=age;
- Cast: A forced conversion operator is required, and the cost of the missing precision needs to be paid for by the large variable.
- Double→float:
double speed=10.4f;float minSpeed=(float)speed;
string num="123";int n=int.Parse(num);int m=Convert.ToInt16(num);
BOOL Type
- BOOL Data type: Non-true false, in the C # language, the BOOL keyword is an alias of System.Boolean, used to declare a variable to store a Boolean value of True and false. There is no reciprocal conversion between the bool type and other types in C #.
Relational operators
- Relational operators:> >= < <= = =! =
- Mainly used for comparison operations, the comparison is true and false with the BOOL type variable storage.
logical operators
- && (Logic and): the expression on both sides of the operator is true and the result is true;
- || (logical OR): At the same time false, the result is false;
- ! (logical): Reverse the result of an expression.
If statement
Usage is the same as C language
Conditional operator: conditional expression? Expression 1: Expression 2
Example: int c=a>b?a:b
- To make an example of a simple if statement
namespace ConsoleApplication1Demo1{ class MainClass { public static void Main(string[] args) { int num = Console.Read(); if (num > 10) { Console.WriteLine(num-10);} else if(num>0){Console.WriteLine(num);} else { Console.WriteLine(num + 10); } } }}
Can be seen (Figure 1), the result is not correct, but the sense of logic is not wrong, a closer look, read can only read the next character, but it is not readline. Actually went into a misunderstanding, always feel that the definition of int is read into the int, the string is not implicitly converted to int, regardless of read or readline, the character type to int type to normal operation, so to be int num = Console.Read();
changed toint num=Convert.ToInt16(Console.ReadLine());
For example, run correctly (Figure 2)
Switch statement
- swich--switch, case--, case the label can only be a shape constant or integer constant expression, cannot be a variable; the entire default statement can be dropped; multiple cases can run a block of statements.
IS and as operators
- Is: Checks whether the object is compatible with the given type;
- The AS: operator is similar to a cast operation to check for certain types of conversions between compatible reference types, and if the conversion is not possible, the SA returns null without throwing an exception.
- Usage examples:
int a=12000;int b=0;if(a is int){b=a;Console.WriteLine(b);string a="b";string b;b=a as string;Console.WriteLine(b);}
C # Basic Primer One