page contents

C#入门教程

本文讲述了C#入门教程!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-11-x69eGYq263719c8755ea0.jpg本文讲述了C#入门教程!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

一、C#的发展历史

C#是.net开发平台支持的多种语言中的一门编程语言。是Microsoft专门为.net

平台开发的一种新的编程语言,语法类似于C语言。

1998年,Delphi设计的第一个C#语言版本

2002年,发布了第一个C#版本(1.0)

二、C#能干什么

1.Windows窗口应用程序。Microsoft office,Windows外观应用程序(按钮,工具)

2.web应用程序(邮箱、论坛、网站)

3.网络数据库等应用程序

4.web服务以及各种分布应用程序

三、C#程序结构

一个C#程序主要包括以下部分:

1.命名空间声明

2.一个class

3.方法

4.属性

5.Main方法

6.语句,表达式

7.注释


using System;–>引入System命名空间(C语言:#include<stdio.h>)

using System.Collections.Generic;–>引入C#泛型命名空间

using System.Linq;–>主要作用是对集合进行查询

using System.Text;–>输出文本类型

using System.Threading.Tasks;–>异步操作


namespace cxsw–>创建命名空间

{

class demo1–>创建一个名为demo1的类

{

static void Main(string[] args)–>main方法,程序的入口

{

}

}

}


main方法的四种写法:

1)static void Main(){}string[] args

2)static void Main(){}

3)static int Main(String[] args){}

4)static int Main(){}


注意:

C#的主方法名第一个字母必须大写:Main(),且必须有static关键字

四.从控制台输出:

1.输出字符串常量

Console.WriteLine(“字符串常量”);

2.输出单个变量

Console.WriteLine(变量);

3.使用连接符(+)混合输出字符串和变量

Console.WriteLine(“字符串”+变量);

4.使用格式占位符混合输出字符串和变量

Console.WriteLine(“字符串{0},{1}”,表达式1,表达式2);

五.从控制台输入:

1)输入字符串类型

定义一个字符串类型的变量,接受用户输入的字符串

String name;

给用户一个提示,提示用户进行输入

Console.WriteLine(“请输入姓名:”);

将用户输入的内容存入变量中

name=Console.Readline();

2)把字符串转化成数字

age=int.Parse(Console.Readline());

六.注释

注释方式:

单行注释://

多行注释:/……/

文档注释:/// xml注释

using System;namespace cxsw { //创建命名空间

class Test101HelloWorld { //创建类

static void Main(string[] args) { //主函数

//输出

Console.WriteLine(“今天第一天”); //控制台输出

int i = 1;

Console.WriteLine(i); //输出变量

Console.WriteLine(“i的值为:”+i);

Console.WriteLine("1+1={0},22={1}",1+1,22);

//输入

string name;

Console.WriteLine(“请输入你的名字:”);

name = Console.ReadLine();

Console.WriteLine(“用户输入的姓名为:”+name);

//把字符串转换为数字

Console.WriteLine(“请输入要转换为字符串的数字:”);

int age = int.Parse(Console.ReadLine());

Console.WriteLine(“被转换为数字的字符串为:”+age);


        //Console.ReadKey();//针对VS.NET用户的

        //这使得程序会等待一个按键的动作,防止程序从VisualStudio.NET

        //启动时屏幕会快速运行并关闭的问题

        //也就是说加上他程序执行最后会等待你按键才退出

    }

}}

第二章:数据类型常量变量

一.数据类型

(1)整数类型

数学上的整数可以从负无穷到正无穷,但是计算机的存储单位是有限的,

所以计算机语言提供的整数类型的值总是一定范围之内的。

C#有八种数据类型:

短字节型(sbyte),字节型(byte),短整型(stort),无符号短整型(ustort),

整型(int),无符号整型(unit),长整型(long),无符号长整型(ulong)。


(2)字符类型

除了数字,计算机处理的信息还包括字符。字符主要包括数字字符,英文字符,

表达式字符等,C#提供的字符类按照国际上公认的标准,采用Unicode字符集。

字符型书数据占用两个字节的内存,可以用来存储Unicode字符集当中的一个字符(

注意,只是一个字符,不是一个字符串)。


(3)实数类型

C#有三种实数类型:float(单精度型),doudle(双精度型),decimal(十进制小数型)。


(4)布尔类型

布尔类型是用来表示“真”和“假”两个概念的,在C#里用true和false来表示。


1.常用数据类型

整数:(4种)

int(32位整数)、short(16)、long(64)、byte(8)

浮点型:(3种)

float(32位浮点数,精确到小数点后7位)

double(64位浮点数,精确到小数点后15~20位)

decimal(128位浮点数,精确到小数点后28~29位)

布尔型:bool true、false

字符型:char(单个字符,用单引号存储)

字符串类型:string(双引号)


2.数据类型转换

数据类型之间的大小关系:

byte–>short–>int–>float–>double–>decimal

1.隐式类型转化

由低类型向高类型的转化

注意:转化的两种数据类型之前必须兼容

2.显式类型转化(强制类型转化)

由高类型向低类型的转化

1)利用Parse方法进行转化

double d1=2.23;

int i1=int.Parse(d1);

2)使用convert提供的类进行强制转化

语法:

ToDouble(变量)


double d = 23.5;

int i;

i = (int)d;

3.引用类型

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;

namespace Csp个人课程备课.第二章数据类型变量{

class Test201数据类型

{

static void Main(string[] args)

{

//数据类型

int age;//用来表示年龄

double score;//用来表示分数

char gender;//用来表示性别

bool b;//用来表示真假

}

}}


二.常量

语法:

const 数据类型 变量名 = 常量值;

在声明和初始化变量的时候,在变量前加上关键字const,就可以把一个变量指定为常量。

在使用过程当中,常量值不会发生变化,后期不允许修改常量值,常量的命名规范和变量是一样的。

using System;namespace Csp个人课程备课.第二章数据类型变量{

class Test202 {

const double pi = 3.14; //定义常量

static void Main(string[] args) {

Console.WriteLine(pi);

}

}}

三.变量

1.变量的声明

变量类型 变量名;

int num;

变量类型 变量名 = 变量值;

房间类型 房间号 = 入住客人;

2.变量的命名

1)驼峰命名法:最中间单词的首字母是大写的

demodirect–>demoDirect

2)帕斯卡命名法:每个单词的首字母都大写

demodirect–>DemoDirect

3.变量的命名规则

1)由字母、数字、下划线组成,不能以数字开头

2)不能用保留字作为变量名(console)

3)建议用英文单词

4)变量名区分大小写

4.注意事项:

1)未经声明的变量不能使用

2)未经赋值的变量不能输出

3)可以一次性声明多个变量

4)可以先声明再赋值,也可以声明的同时初始化

using System;namespace Csp个人课程备课.第二章数据类型变量{

class Test203 {

static void Main(string[] args) {

//定义在函数体当中的变量为局部变量 只能在当前函数体内使用

//定义变量

int a;

a = 10;

Console.WriteLine(“变量a的值为:”+ a);

int b = 20;

Console.WriteLine(“变量b的值为:”+b);

b = 40;

Console.WriteLine(“变量b修改后的值为:”+ b);

int c, d;

c = 3;

d = 4;

Console.WriteLine(“变量c的值为:”+c+“,变量d的值为:”+d);

int e = 1, f = 2;

Console.WriteLine(“变量e的值为:”+e+“,变量f的值为:”+f);

}

}}

using System;namespace Csp个人课程备课.第二章数据类型变量{

class Test204 {

static int i; //定义在函数体外的变量为全局变量 可以在当前文件中使用

static void Main(string[] args)

{

//定义在函数体当中的变量为局部变量 只能在当前函数体内使用

//定义变量

int a = 10;

Console.WriteLine(“变量a输出:”+a);


i = 30;

Console.WriteLine("常量输出:"+i);

}

void eat(){

//a = 11; //提示未定义访问不到

i = 31; //不提示错误证明可以访问到

}


}}

四.注意事项

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;

namespace Csp个人课程备课.第二章数据类型变量{

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;//选用数据类型时 注意是否超出范围

//如果超出范围则错误


float ff = 3.14f; //浮点数类型后面加f

double dd = 3.14;

Console.WriteLine(ff + " " + dd);


//char类型

char cs = 'a';//char类型的值要用单引号引起来

char cc = ' ';//char类型的值不能啥也没有哪怕是一个空格也可以

char ccc = '\n';//表示转义字符

char c1 = (char)97;//ASCII码

Console.WriteLine(cs+" "+cc+" "+ccc+" "+c1);


//数据类型转换

short st1 = 2;

int it1 = st1;//隐式转换

Console.WriteLine("it1的值为:"+it1);

double db1 = 25.5;

int it2 = (int)db1;//强制转换

Console.WriteLine("it2的值为:"+it2);

}

}}

五.ASCII码表

六.转义字符

第三章:运算符

一.算数运算符


/ % ++ –

+号的用法:

1)对两个数字进行相加

2)字符串的连接

++、–复合运算:

1)++在前:先运算,后赋值

2)++在后:先赋值,后运算

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;

namespace Csp个人课程备课.第三章运算符和表达式{

class Test301算数运算符{

static void Main(string[] args) {

int A = 10, B = 5;

//算数运算符的各种运算

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));

//取模,表达式中书写了A%%B,其中两个%表示输出一个%

Console.WriteLine(“A%B=”+(A % B));

 int a = 1, b = 2, c = 3, d = 0;

 Console.WriteLine("a,b,c的值为:"+a+" "+b+" "+c);

 Console.WriteLine("a++的值为:"+(a++));

 Console.WriteLine("a的值为:"+a);

 Console.WriteLine("++a的值为:"+(++a));

 Console.WriteLine("a的值为:"+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;  //在下一次计算时才会得到所有加一后的值

 Console.WriteLine("d的值为:"+d);//12

}

}}

二.关系运算符

(比较是否相等) != > < >= <=

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**

关系运算符

== != > < >= <=

**/namespace Csp个人课程备课.第三章运算符和表达式{

class Test302关系运算符

{

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

}

}}

三.逻辑运算符

&& || !

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**

逻辑运算符

&&(与):符号两边同时成立 则 成立

||(或):只要有个成立 则 成立

!(非): 结果相反

**/namespace Csp个人课程备课.第三章运算符和表达式{

class Test303逻辑运算符

{

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

}

}}

四.赋值运算符

(赋值) += -= = /= %=

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/*

赋值运算符

= += -= *= /= %=

**/namespace Csp个人课程备课.第三章运算符和表达式{

class Test304赋值运算符

{

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

}

}}

五.位运算

&(按位与) |(或) ^(异或) ~(非) <<(左移) >>(右移)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**

位运算符

 &(按位与)   |(或)^(异或)~(非)   <<(左移)>>(右移) >>>(无符号右移)

 二进制

 十进制转换二进制

 13二进制1101

 20481024512  256128 64 32  16   8  4  2  1 权次方

    1  1  0  1

 1541     0   0    1   1  0  1  0

 6851       0   1     0   1    0   1  1  0  1

 12501       0       0   1     1   1    0   0  0  1  0

 243                         1     1   1    1   0  0  1  1

 684             1       0   1     0   1    0   1  1  0  0

 60                                    1    1   1  1  0  0

 //如243可以减128用得到就是1余数再往后减可以减就是1,不够减就是0

 符号位  1代表负数   0 代表 正数

**/namespace Csp个人课程备课.第三章运算符和表达式{

class Test305位运算符

{

static void Main(string[] args) {

int a = 60;

//二进制:111100

int b = 13;//二进制:001101 1101前面的0是补上去的 正数补0负数补1 为了和要比较的二进制位数一致

int c = 0; //

c = a & b; // 二进制:001100 相对应的位都为1时结果为1

Console.WriteLine©;//12


c = a | b;//二进制:111101相对应的只要有一个为1结果为1 111101

Console.WriteLine(c);//61


c = a ^ b;//二进制:110001相对应的位 相同为0 不同为1

Console.WriteLine(c);//49


c = ~a;   //二进制:000011 相对应的位  取反

Console.WriteLine(c);//-61


c = a << 2; //二进制:11110000

Console.WriteLine(c);//240


c = -a >> 60;//二进制:二进制 32个1

Console.WriteLine(c);//-1

}

}}

六.条件运算符

(三元运算符) 表达式1?表达式2:表达式3 -> 3>2?3:2

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/**

三目运算符 如果 否则

 1? 2: 3

 1:条件2:条件成立的话要做的事3.条件不成立要做的事**/namespace Csp个人课程备课.第三章运算符和表达式{

class Test306三元运算符

{

static void Main(string[] args) {

int a = 10;

int b;

b = (a > 10) ? 100 : 50; //如果a大于10 条件 则 成立为100,否则 为 50.

Console.WriteLine(b);//条件不成立所以50

}

}}

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-11-14 09:40
  • 阅读 ( 248 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章