Knowledge point One main function, data type, type conversion

Source: Internet
Author: User

I. Contents of the main function

Output, input

Output:
Console.WriteLine ();
Console.Write ();
1. Output Digital
Console.WriteLine (22);//22
Console.WriteLine (12+44);//56
2. Output text
Console.WriteLine ("ABC");//abc
Console.WriteLine ("78" + "56");//7856
3. Integrated output
Console.WriteLine ("78+56=" + (78 + 56));//78+56=134①

Console.Write ("78+56=");
Console.WriteLine (78 + 56); Ii

The effect of ① effect =②

Input:
String AAA = Console.ReadLine ();

Integrated applications:
Input
string s = Console.ReadLine ();
Output
Console.WriteLine ("What you just entered is:" +s);


Case 1:
Make an adder:
Display: Please enter a number.
Wait for input
Display: Please enter another number
Wait for input
Displays the results of the final operation.

Namespace ConsoleApplication2
{
Class Program

{

static void Main (string[] args)
{Console.WriteLine ("Enter a number");
String A=console.readline ();
Console.WriteLine ("Enter another number");
String B=console.readline ();
Double C =convert.todouble (a) +convert.todouble (b);
Console.WriteLine ("{0}+{1}={2}", a,b,c);
}

}

}

The effect is as follows:

Enter a number

5

Enter a different number

6

5+6=11

Case 2:

static void Main (string[] args)
{
Console.WriteLine ("******** Information Survey Form *********"); Show title
Console.Write ("Name:");
String xm = Console.ReadLine ();
Console.Write ("Gender:");
String xb = Console.ReadLine ();
Console.Write ("Graduate School:");
String byxx = Console.ReadLine ();
Console.Write ("Major in Study:");
String sxzy = Console.ReadLine ();

Show:
Console.WriteLine ("********* just entered the following ************");
Console.WriteLine ("Name:" +xm+ "Gender:" +XB);
Console.WriteLine ("Graduate School:" +BYXX);
Console.WriteLine ("Major in Study:" +sxzy);
}

Case 3:

static void Main (string[] args)
{
Console.WriteLine ("Little hi: What's your name?") ");
Console.Write ("I:");
String xm = Console.ReadLine ();
Console.WriteLine ("Small hi: Oh, so you are the" +xm+ "Ah, too! , what do you like to eat? ");
Console.Write ("I:");
string sw = Console.ReadLine ();
Console.WriteLine ("Small hi: I also like to eat" +sw+ ", you can eat how much ah?) ");
Console.Write ("I:");
String sl = Console.ReadLine ();
Console.WriteLine ("Small hi: you actually eat" +sl+ ", than I eat a lot of");


}

Second, the data type:
1. Basic type
Integer int can only put a finite number of digits-xxxx ~ xxxx, one less than the phone number.
Long
Short

Floating-point Double default decimal number is double
Float if the decimal is to be seen as float, you need to add the suffix ' f,f '
float A = 3.14f;float a = 3.14;//error

Character Char in single quotation marks, only one character can be placed. Except for escape characters
char c = ' a '; char c = "a";//Wrong char c= ' ABC ';//Wrong

boolean bool can only put true,false. The code can only be lowercase and not quoted.
BOOL g = true;bool g = true;//wrong bool g = "true";//Wrong

2. Reference type
String double quotation marks, consisting of one or more characters.
string s = "abcdef"; string s = "a"; string s = ' asdfasdf ';//Wrong

Second, type conversion:
1. Automatic conversion-as long as there is no possibility of data loss, will automatically turn.
2. Casting
For numbers: Add parentheses to the left of the converted value, and write in parentheses about which type to turn to.
float a = (float) 3.14;
For string: The base type to which the string is converted
Method one: int a = Int. Parse ("string"); Float B = float. Parse ("string"); Double c = Double. Parse ("string"); bool D = bool. Parse ("string")
Law II: Using Convert
Double D = convert.todouble (s); float F = convert.tosingle (s);
int a = Convert.ToInt32 (s);

Case 1:

static void Main (string[] args)
{

string s = "4.5";
Double d = Double. Parse (s);
Double D = convert.todouble (s);

Console.WriteLine (d+1);

string s = "4.5";
Double d = Double. Parse (s);
int a = (int) d;
Console.WriteLine (a);

int a = 10;
Float B = (float) 9.8;
Double C = a + B;

A = (int) c;

Console.WriteLine (a);

int a = 4;
Long B = 19;

b = A;

Double c = 3.14;

float F = 2.22f;

c = f;

Char d = ' \a ';
Console.WriteLine (d);

bool G = true;
g = false;
Console.WriteLine (g);

}

Case 2:

static void Main (string[] args)
{

string s = "true";
BOOL B = Convert.toboolean (s);

b =!b;

Console.WriteLine (b);

}

Case 3:


static void Main (string[] args)
{
Console.Write ("How old are you this year?") ");
string s = Console.ReadLine ();
Double age1 = convert.todouble (s);
int age2 = (int) age1;
Console.WriteLine ("You are now {0} years old, next year {1} years old", Age2 + 1,age2);
}

Case 4:

static void Main00 (string[] args)
{

string s = "C:\\text\\new\\aaa.txt";
Console.WriteLine (s);
string s = "Note: \" Puppy "is used to denote intimacy";
Console.WriteLine (s);

char c = ' A '; ASCII code
int b = (int) c;
Console.WriteLine (b);

int a = 43;
char C = (char) A;

Console.WriteLine (c);

for (int i = 0; i <; i++)
//{
char C = (char) i;
Console.Write (i+ "=" +c+ "\ t");
//}
}

Note:

C # escape character: \ ' single quotation mark

\ "Double quotation marks

\\    \

Air

\a Warning

\ nthe line break

\ r Enter

\ t Horizontal tab

Beginner C # language, just a feeling, programmers really not simple.

Knowledge point One main function, data type, type conversion

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.