当前位置:网站首页>C Getting Started tutorial
C Getting Started tutorial
2022-06-25 07:39:00 【Hask】
C# Introduction to small tutorial
One 、C# The history of development
C# yes .net One of several languages supported by the development platform . yes Microsoft Specially for .net
A new programming language for platform development , The syntax is similar to C Language .
1998 year ,Delphi First design C# Language version
2002 year , Released the first C# edition (1.0)
Two 、C# Can do
1.Windows Window application .Microsoft office,Windows Appearance Application ( Button , Tools )
2.web Applications ( mailbox 、 Forum 、 Website )
3. Network database and other applications
4.web Services and various distributed applications
3、 ... and 、C# Program structure
One C# The procedure mainly includes the following parts :
1. Namespace declaration
2. One class
3. Method
4. attribute
5.Main Method
6. sentence , expression
7. notes
using System;–> introduce System Namespace (C Language :#include<stdio.h>)
using System.Collections.Generic;–> introduce C# Generic namespace
using System.Linq;–> The main function is to query the collection
using System.Text;–> Output text type
using System.Threading.Tasks;–> Asynchronous operations
namespace cxsw–> Create a namespace
{
class demo1–> Create a file called demo1 Class
{
static void Main(string[] args)–>main Method , Program entry
{
}
}
}
main There are four ways to write a method :
1)static void Main(){}string[] args
2)static void Main(){}
3)static int Main(String[] args){}
4)static int Main(){}
Be careful :
C# The first letter of the main method name must be capitalized :Main(), And there must be static keyword
Four . Output from console :
1. Output string constant
Console.WriteLine(“ String constant ”);
2. Output a single variable
Console.WriteLine( Variable );
3. Use connectors (+) Mix output strings and variables
Console.WriteLine(“ character string ”+ Variable );
4. Use format placeholders to mix output strings and variables
Console.WriteLine(“ character string {0},{1}”, expression 1, expression 2);
5、 ... and . Input... From the console :
1) Input string type
Define a variable of type string , Accept the string entered by the user
String name;
Give the user a hint , Prompt the user to enter
Console.WriteLine(“ Please enter a name :”);
Store the user's input into variables
name=Console.Readline();
2) Convert a string into a number
age=int.Parse(Console.Readline());
6、 ... and . notes
Annotation mode :
Single-line comments ://
Multiline comment :/……/
Documentation Comments :/// xml notes
using System;namespace cxsw { // Create a namespace
class Test101HelloWorld { // Create a class
static void Main(string[] args) { // The main function
// Output
Console.WriteLine(“ Today's first day ”); // Console output
int i = 1;
Console.WriteLine(i); // Output variables
Console.WriteLine(“i The value of is :”+i);
Console.WriteLine("1+1={0},22={1}",1+1,22);
// Input
string name;
Console.WriteLine(“ Please enter your name :”);
name = Console.ReadLine();
Console.WriteLine(“ The name entered by the user is :”+name);
// Convert a string to a number
Console.WriteLine(“ Please enter a number to convert to a string :”);
int age = int.Parse(Console.ReadLine());
Console.WriteLine(“ The string that is converted to a number is :”+age);
//Console.ReadKey();// in the light of VS.NET User
// This causes the program to wait for a key action , Prevent the program from VisualStudio.NET
// The screen will run quickly and close when starting
// That is to say, the program execution will wait for you to press the key before exiting
}
}}
Chapter two : Data type constant variable
One . data type
(1) Integer types
Mathematical integers can range from negative infinity to positive infinity , But the storage unit of the computer is limited ,
Therefore, the value of integer type provided by computer language is always within a certain range .
C# There are eight data types :
Short byte type (sbyte), Byte type (byte), Short (stort), Unsigned short (ustort),
integer (int), Unsigned integer (unit), Long integer (long), Unsigned long (ulong).
(2) Character type
In addition to digital , The information processed by the computer also includes characters . Characters mainly include numeric characters , English characters ,
Expression characters, etc ,C# The character classes provided are in accordance with internationally recognized standards , use Unicode Character set .
Character book data occupies two bytes of memory , It can be used to store Unicode A character in a character set (
Be careful , Just one character , Not a string ).
(3) Real number type
C# There are three types of real numbers :float( Single precision type ),doudle( Double precision type ),decimal( Decimal type ).
(4) Boolean type
Boolean type is used to represent “ really ” and “ false ” Of two concepts , stay C# In the use true and false To express .
1. Common data types
Integers :(4 Kind of )
int(32 An integer )、short(16)、long(64)、byte(8)
floating-point :(3 Kind of )
float(32 Bit floating point , Accurate to the decimal point 7 position )
double(64 Bit floating point , Accurate to the decimal point 15~20 position )
decimal(128 Bit floating point , Accurate to the decimal point 28~29 position )
Boolean type :bool true、false
Character :char( Single character , Store in single quotation marks )
String type :string( Double quotes )
2. Data type conversion
The size relationship between data types :
byte–>short–>int–>float–>double–>decimal
1. Implicit type transformation
The transformation from low type to high type
Be careful : The two data types to be converted must be compatible
2. Explicit type conversion ( Forced type conversion )
The transformation from high type to low type
1) utilize Parse Methods to transform
double d1=2.23;
int i1=int.Parse(d1);
2) Use convert Forced conversion of the provided class
grammar :
ToDouble( Variable )
double d = 23.5;
int i;
i = (int)d;
3. Reference type
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace Csp Personal course preparation . Chapter 2 data type variables {
class Test201 data type
{
static void Main(string[] args)
{
// data type
int age;// Used to indicate age
double score;// Used to express scores
char gender;// Used to indicate gender
bool b;// Used to indicate true or false
}
}}
Two . Constant
grammar :
const data type Variable name = Constant values ;
When declaring and initializing variables , Add the keyword before the variable const, You can specify a variable as a constant .
In the process of using , Constant values do not change , Constant values are not allowed to be modified later , The naming conventions of constants and variables are the same .
using System;namespace Csp Personal course preparation . Chapter 2 data type variables {
class Test202 {
const double pi = 3.14; // Define constants
static void Main(string[] args) {
Console.WriteLine(pi);
}
}}
3、 ... and . Variable
1. Declaration of variables
Variable type Variable name ;
int num;
Variable type Variable name = A variable's value ;
Room type Your room number, = Check in guests ;
2. Variable name
1) Hump nomenclature : The first letter of the middle word is capitalized
demodirect–>demoDirect
2) PASCAL nomenclature : The first letter of each word is capitalized
demodirect–>DemoDirect
3. Naming rules for variables
1) By letter 、 Numbers 、 Underline composition , Cannot start with a number
2) You cannot use reserved words as variable names (console)
3) English words are recommended
4) Variable names are case sensitive
4. matters needing attention :
1) Undeclared variables cannot be used
2) Unassigned variables cannot be output
3) You can declare multiple variables at once
4) You can declare it before you assign it , It can also be declared and initialized at the same time
using System;namespace Csp Personal course preparation . Chapter 2 data type variables {
class Test203 {
static void Main(string[] args) {
// The variables defined in the function body are local variables Can only be used in the body of the current function
// Defining variables
int a;
a = 10;
Console.WriteLine(“ Variable a The value of is :”+ a);
int b = 20;
Console.WriteLine(“ Variable b The value of is :”+b);
b = 40;
Console.WriteLine(“ Variable b The modified value is :”+ b);
int c, d;
c = 3;
d = 4;
Console.WriteLine(“ Variable c The value of is :”+c+“, Variable d The value of is :”+d);
int e = 1, f = 2;
Console.WriteLine(“ Variable e The value of is :”+e+“, Variable f The value of is :”+f);
}
}}
using System;namespace Csp Personal course preparation . Chapter 2 data type variables {
class Test204 {
static int i; // The variables defined outside the function are global variables You can use... In the current file
static void Main(string[] args)
{
// The variables defined in the function body are local variables Can only be used in the body of the current function
// Defining variables
int a = 10;
Console.WriteLine(“ Variable a Output :”+a);
i = 30;
Console.WriteLine(" Constant output :"+i);
}
void eat(){
//a = 11; // Prompt undefined access is not available
i = 31; // No error is prompted to prove that you can access
}
}}
Four . matters needing attention
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace Csp Personal course preparation . Chapter 2 data type variables {
class Test205{
static void Main(string[] args)
{
short s = 1;
int i = 2;
long l = 3;
char ch = ‘a’;
Console.WriteLine(s + " " + i + " " + l + " " + ch);
//i = 2200000000;// When selecting data type Note whether it is out of range
// If out of range, error
float ff = 3.14f; // Floating point number type followed by f
double dd = 3.14;
Console.WriteLine(ff + " " + dd);
//char type
char cs = 'a';//char The value of type should be enclosed in single quotation marks
char cc = ' ';//char The value of type can't have nothing, even a space
char ccc = '\n';// Indicates the escape character
char c1 = (char)97;//ASCII code
Console.WriteLine(cs+" "+cc+" "+ccc+" "+c1);
// Data type conversion
short st1 = 2;
int it1 = st1;// Implicit conversion
Console.WriteLine("it1 The value of is :"+it1);
double db1 = 25.5;
int it2 = (int)db1;// Coercive transformation
Console.WriteLine("it2 The value of is :"+it2);
}
}}
5、 ... and .ASCII clock
6、 ... and . Escape character
The third chapter : Operator
One . Arithmetic operator
/ % ++ –
+ The usage of number :
1) Add two numbers
2) String connection
++、– Compound operation :
1)++ before : Calculate first , Post assignment
2)++ After : Assign first , Post operation
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace Csp Personal course preparation . Chapter 3 operators and expressions {
class Test301 Arithmetic operator {
static void Main(string[] args) {
int A = 10, B = 5;
// Various operations of arithmetic operators
Console.WriteLine(“A=10,B=5”);
Console.WriteLine(“A+B=”+(A + B));
Console.WriteLine(“A-B=”+(A - B));
Console.WriteLine(“A*B=”+(A * B));
Console.WriteLine(“A/B=”+(A / B));
// modulus , The expression says A%%B, Two of them % Indicates the output of a %
Console.WriteLine(“A%B=”+(A % B));int a = 1, b = 2, c = 3, d = 0; Console.WriteLine("a,b,c The value of is :"+a+" "+b+" "+c); Console.WriteLine("a++ The value of is :"+(a++)); Console.WriteLine("a The value of is :"+a); Console.WriteLine("++a The value of is :"+(++a)); Console.WriteLine("a The value of is :"+a); a = 1; b = 2; c = 3; d = 0; //a = 3; b = 3; c = 3; // 1 2 3 3 3 d = a++ + a + ++b + b + ++a; // In the next calculation, we will get all the values after adding one Console.WriteLine("d The value of is :"+d);//12}
}}
Two . Relational operator
( Comparison is equal ) != > < >= <=
1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**
- Relational operator
- == != > < >= <=
**/namespace Csp Personal course preparation . Chapter 3 operators and expressions {
class Test302 Relational operator
{
static void Main(string[] args) {
int a = 20; int b = 20; int c = 30;
Console.WriteLine(a == b);//true
Console.WriteLine(a != b);//false
Console.WriteLine(a > b);//false
Console.WriteLine(a > c);//false
Console.WriteLine(c > b);//true
Console.WriteLine(a < c);//true
Console.WriteLine(a >= c);//false
Console.WriteLine(a <= c);//true
}
}}
3、 ... and . Logical operators
&& || !
1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**
- Logical operators
- &&( And ): Both sides of the symbol hold at the same time be establish
- ||( or ): As long as one is established be establish
- !( Not ): The result is the opposite
**/namespace Csp Personal course preparation . Chapter 3 operators and expressions {
class Test303 Logical operators
{
static void Main(string[] args) {
bool a = true;
bool b = false;
bool c = true;
Console.WriteLine(a && b);//false
Console.WriteLine(b || c);//true
Console.WriteLine(!c);//false
int d = 1;
int e = 2;
int f = 3;
Console.WriteLine(d > e && f < e);//false
Console.WriteLine(e < f || d > f);//true
Console.WriteLine(!(f > d));//false
}
}}
Four . Assignment operator
( assignment ) += -= = /= %=
1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/*
- Assignment operator
- = += -= *= /= %=
**/namespace Csp Personal course preparation . Chapter 3 operators and expressions {
class Test304 Assignment operator
{
static void Main(string[] args) {
int a = 10;
int b = 20;
int c;
c = a + b;
Console.WriteLine;//30
c += a;
Console.WriteLine;//40
c -= a;
Console.WriteLine;//30
c /= 1;
Console.WriteLine;//30
b %= 2;
Console.WriteLine(b);//0
}
}}
5、 ... and . An operation
&( Bitwise AND ) |( or ) ^( Exclusive or ) ~( Not ) <<( Move left ) >>( Move right )
1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**
- An operator
&( Bitwise AND ) |( or ) ^( Exclusive or ) ~( Not ) <<( Move left ) >>( Move right ) >>>( unsigned right shift )Binary systemDecimal conversion binary13 Binary system 11012048 1024 512 256 128 64 32 16 8 4 2 1 Weighted power1 1 0 1154 1 0 0 1 1 0 1 0685 1 0 1 0 1 0 1 1 0 11250 1 0 0 1 1 1 0 0 0 1 0243 1 1 1 1 0 0 1 1684 1 0 1 0 1 0 1 1 0 060 1 1 1 1 0 0// Such as 243 You can reduce 128 What you can use is 1 The remainder can be reduced by further subtraction 1, Not enough minus is 0Sign bit 1 It's a negative number 0 representative Positive numbers
**/namespace Csp Personal course preparation . Chapter 3 operators and expressions {
class Test305 An operator
{
static void Main(string[] args) {
int a = 60;
// Binary system :111100
int b = 13;// Binary system :001101 1101 Ahead 0 It was made up Positive complement 0 Negative numbers make up 1 In order to be consistent with the binary digits to be compared
int c = 0; //
c = a & b; // Binary system :001100 The corresponding bits are 1 The result is 1
Console.WriteLine;//12
c = a | b;// Binary system :111101 As long as there is one corresponding to 1 The result is 1 111101
Console.WriteLine(c);//61
c = a ^ b;// Binary system :110001 Corresponding bit Same as 0 Different for 1
Console.WriteLine(c);//49
c = ~a; // Binary system :000011 Corresponding bit Take the opposite
Console.WriteLine(c);//-61
c = a << 2; // Binary system :11110000
Console.WriteLine(c);//240
c = -a >> 60;// Binary system : Binary system 32 individual 1
Console.WriteLine(c);//-1
}
}}
6、 ... and . Conditional operator
( Ternary operator ) expression 1? expression 2: expression 3 -> 3>2?3:2
1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**
- Ternary operator If otherwise
1? 2: 3
class Test306 Ternary operator1: Conditions 2: What to do if the conditions are met 3. What to do if the condition is not established **/namespace Csp Personal course preparation . Chapter 3 operators and expressions {
{
static void Main(string[] args) {
int a = 10;
int b;
b = (a > 10) ? 100 : 50; // If a Greater than 10 Conditions be Established as 100, otherwise by 50.
Console.WriteLine(b);// The condition doesn't hold, so 50
}
}}
边栏推荐
- Alphassl wildcard certificate for one month
- 音频(五)音频特征提取
- OAuth 2.0一键登录那些事
- VectorDraw Web Library 10.10
- [batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
- Domestic MCU perfectly replaces STM chip model of Italy France
- 13 `bs_duixiang.tag标签`得到一个tag对象
- Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
- Vscode official configuration synchronization scheme
- Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
猜你喜欢

【批处理DOS-CMD命令-汇总和小结】-外部命令-cmd下载命令、抓包命令(wget)

Advanced mathematics foundation_ Parity of functions

Ltpowercad II and ltpowerplanner III

Chuantuwei ca-is3720lw alternative material No. iso7820fdw

【批处理DOS-CMD命令-汇总和小结】-上网和网络通信相关命令(ping、telnet、nslookup、arp、tracert、ipconfig)

Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market

Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver

PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering

为什么要“除夕”,原来是内存爆了!
![对链表进行插入排序[dummy统一操作+断链核心--被动节点]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
随机推荐
Zhugeliang vs pangtong, taking distributed Paxos
我的处女作杀青啦!
Path planner based on time potential function in dynamic environment
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
用太极拳讲分布式理论,真舒服!
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Advanced mathematics foundation_ Parity of functions
Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
[QT] shortcut key
Mysql database import SQL file display garbled code
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
College entrance examination voluntary filling, why is the major the last consideration?
Redis learning notes
[introduction to UVM== > episode_9] ~ register model, integration of register model, general methods of register model, application scenarios of register model
TEMPEST HDMI泄漏接收 2
Let's talk about MCU crash caused by hardware problems
lebel只想前面有星号,但是不想校验
Ns32f103c8t6 can perfectly replace stm32f103c8t6