C # Basic Primer One

Source: Internet
Author: User

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类的输出方法    
    • Console.Write ("{0} World", hello), equivalent to direct output

    • Input function
      • Console.read ();//Read the next character from the standard input stream
      • Console.readkey ();//read a character or function key
      • Console.ReadLine ();//reads the next line of characters from the standard input stream
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→int:
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

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.