[C# Introduction] Day 1: helloworld and data types

Recently, the author (someone from ancient csdn) wants to make some small windows applications. After some understanding, C# + winform or C# + wpf is fast and easy to use, so I started my C# learning journey. I am afraid that after studying by myself, I will forget something if I don’t need it for a long time, so there is a series of [C# Introduction]~ This
article is most suitable for students who have learned other programming languages ​​but have no C# programming foundation to get started quickly.

1. Visual studio installation and environment configuration

1. Installation conditions

Windows: win7 and above (xp does not have .net framework, additional installation is required, and the probability of problems is high)
memory: 8G and above (the bigger the better)
installation package: download and install by yourself, just keep going to the next step, it is recommended Do not install on system disk

2. Environment variable configuration

a) Confirm the directory of csc

csc.exe compiles the program for c#
64-bit: C:\Windows\Microsoft.NET\Framework64\v4.0.30319
32-bit: C:\Windows\Microsoft.NET\Framework\v4.0.30319
64-bit computer selects the 64-bit directory , choose the 32-bit directory for 32-bit
insert image description here

b) Configure environment variables

Right-click [Computer] - [Advanced System Settings] - [Advanced] - [Environment Variables] - [path] - [Edit] to enter the
directory of csc. If there is no semicolon in front, enter the semicolon first, and then enter the csc Table of contents
insert image description here

2. The first Hello, world!

1. Create a new txt file and modify the suffix to .cs

The .cs file is the code file of c#, which is the same as .java for java and .cpp for c++. The meaning of the suffix is ​​(c#, read c sharp, and take the first two letters cs)

2. Manual code input

using System;

class HelloWord
{
	static void Main() {
		Console.WriteLine("Hello,world!");
		Console.ReadKey();
	}
}

Note:
1. The uppercase S of System is followed by a semicolon
2. The method name is Main, and the capital M is uppercase
3. Console.ReadKey() needs to be added; otherwise the output will flash by

3. Compile and run

In the file directory, press [shift] + [right mouse button], select [open command window here]
compile: input csc your file name.cs
execute: your file name.exe you
can see Hello, world!

3. Code Interpretation

1. using System;

The using in C# can be understood as the include of C language, which is to introduce the system library System
System is called a namespace in C#, which can be understood as a collection of codes, and the codes inside can be stored in different files, as long as these The files all declare the same namespace. Once this namespace is used, all methods under this namespace can be used

2.static void Main() {}

The Main function is the entry function of the cs file. If there is one, the code in the Main function will be executed. If not, it will not be executed. It is the same as the main in C language and Java.

3. Console.WriteLine()

This method is actually System.Console.WriteLine(), and the method is under the System namespace.
The function of the function is to output newlines in the console, which is the same as println in C language and System.out.println in java.

Fourth, the use of Visual studio

[New Project] - [Visual C#] - [Windows] - [Console Application] - Modify the name and location - [OK]
insert image description hereunder the System Explorer window - [Program.cs], click on the edit window on the right modify the code
insert image description here

Five, variable type

1. Variables and constants:

Variable names: start with a letter or underscore, can contain letters, underscores and numbers

int a = 1;

Constant: const decoration, the content is immutable, if modified, an error will be reported

const int a = 1;
a = 2;
Console.WriteLine(a);
Console.ReadKey();

2. Common types

int: integer, 4 digits
ps: If it is an int type obtained from the Internet, because int can store null values ​​in the database, when receiving int type variables, you can use int?, and subsequent bool, float, etc. can be added ?, can store null value.
insert image description here
short: short integer, 2 digits
long: long integer, 8 digits, the range of values ​​that can be represented is larger than int, generally use int, relatively large use long, especially large use decimal bool: Boolean type, only true
and False, c# cannot convert int to bool type, 0 cannot be converted to false
float: single-precision floating-point type
double: double-precision floating-point type, can display 15-16 digits
insert image description here
ps: C# supports {0},{1 } such a formatted output statement, {0} represents the first parameter after the comma, and {1} represents the second parameter after the comma

char: character type, must use single quotes,
string: string type, must use double quotes. Adding @ can keep the original format, which can avoid the loss of format and the need to escape repeatedly when outputting escape characters.
ps: Single and double quotes in php and python can be mixed, but C# can’t do it (single and double quotes in php are slightly different when used in dynamic variables, if you are interested, please use Baidu) Other types byte, sbyte, uint,
insert image description hereushort ,decimal is not very commonly used, please understand if you are interested

3. Common type conversion

string to int: int.Parse() or int.Tryparse()
string to float, double is the same, float.Parse(), double.Parse()
insert image description here
to other types of string: variable name.ToString()

4. string knowledge supplement

string.Empty is equivalent to the empty string "", the string has been created, and the content inside is just empty, which is equivalent to building a house, and people have not yet lived in it. Null means that the string has not been
created, which is equivalent to the house not being built yet.

string x = null;
string y = string.Empty;
string z = "";
Console.WriteLine(x == "");
Console.WriteLine(y == "");
Console.WriteLine(z == "");
Console.ReadKey();

String and StringBuilder, this is also an old-fashioned problem. In the java coding specification, you must use StringBuilder instead of string to dynamically add multiple strings. The same is true for C#.
The result of running with the following code is 100 characters, and 1000 characters
can be ignored . StringBuilder is 1ms faster than string and 10000 characters. StringBuilder is more than 10 times faster than string and 100000 characters. StringBuilder is about 18ms. . .


// namespace在代码顶部添加
using System.Diagnostics;

 // string和stringbuilder,添加字符的时长比较,2万个字符就很明显,10万字符str计算要等很久,stringbuilder 18ms左右,1000字符以内仅相差1ms左右
Stopwatch sw = new Stopwatch();

sw.Start();

//string a = string.Empty;
//for (int i = 0; i < 1000; i++)
//{
//    a += i.ToString();
//}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
    sb.Append(i.ToString());
}


sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadKey();

5. Array

Array: an ordered collection of basic types
The common way of writing an array:

string[] arr1 = new string[]{"123", "abc", "zxy"};
int[] arr2 = new int[10];

Guess you like

Origin blog.csdn.net/guggle15/article/details/123269936