AI智能
改变未来

05c#语言基本元素概览,初始类型,变量与方法,算法简介

构成c#语言的基本元素

1.关键字

2.操作符

3.标识符
学习阅读c#语言文档
大小写规范
驼峰法:头一个单词首字母小写,其余后面每个单词首字母大写。变量名
构成关键字每个单词首字母大写。方法名,类名,名称空间
4.标点符号
;——语句结束
{}
不参与运算
5.文本(字面值)
整数

int x=3;long y=3L;

实数

float x=3.0F;//单精度数,32位表示一个浮点数double y=4.0;//双精度数,64位表是浮点数

默认双精度数,表示单精度数后面必须加F

字符

char c=\'a\';

字符串

string str=\"a\";

布尔

bool b=true;bool b2=false;

空值

string str=null;Form f=null;//空引用出错,System.NullReferenceExceptionf.showdialog();

6.注释和空白
单行注释:

//string str=null;

块注释:

/*string str=null;*/

块注释不能嵌套

ctrl+E,D:格式化代码

初识类型、变量、方法名

类型 Type

var  x=3L;//自动推断出什么类型,变量Console.WriteLine(x,GetType().Name);//int32,int64

变量

int x;x=100;float x;x=3.0F;double x;x=3.0;

方法

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();int x = c.Add(2, 3);Console.WriteLine(x);Console.ReadLine();}}class Calculator{public int Add(int a, int b){int result = a + b;return result;}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();string str = c.Today();Console.WriteLine(str);Console.ReadLine();}}class Calculator{public int Add(int a, int b){int result = a + b;return result;}public string Today(){int day = DateTime.Now.Day;return day.ToString();}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();string str = c.Today();c.PrintSum(4,6); Console.ReadLine();}}class Calculator{public int Add(int a, int b){int result = a + b;return result;}public string Today(){int day = DateTime.Now.Day;return day.ToString();}public void PrintSum(int a,int b) {int result = a + b;Console.WriteLine(result);}}}

算法简介

循环

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();c.PrintXTo1(10);Console.ReadLine();}}class Calculator{public void PrintXTo1(int x){for (int i = x; i >0; i--){Console.WriteLine(i);}}}}

注释
ctrl+kc
取消注释
ctrl+ku

递归

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();c.PrintXTo1(3);Console.ReadLine();}}class Calculator{//public void PrintXTo1(int x)//{//    for (int i = x; i >0; i--)//    {//        Console.WriteLine(i);//    }//}public void PrintXTo1(int x){if (x == 1){Console.WriteLine(x);}else{Console.WriteLine(x);PrintXTo1(x-1);}}}}

计算从1到100的和

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();int result = c.SumFrom1ToX(100);Console.WriteLine(result);Console.ReadLine();}}class Calculator{//public int SumFrom1ToX1(int x)// {//     int result = 0;//     for (int i = 1; i < x+1; i++)//     {//         result = result + i;//     }//     return result;// }//非递归public int SumFrom1ToX2(int x){if (x==1){return 1;}else{int result = x+ SumFrom1ToX(x-1);return result;}}//递归public int SumFrom1ToX3(int x){return (1 + x) * x / 2;}}}

作业:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace demo1{class Program{static void Main(string[] args){Calculator c = new Calculator();int n = 8;c.hannoi(n, \'A\', \'B\', \'C\');Console.ReadLine();}}class Calculator{public void hannoi(int n, char from, char buffer, char to){if (n == 0)return;hannoi(n - 1, from, to, buffer);Console.Write(\"Move disk \");Console.Write(n);Console.Write(\" from \");Console.Write(from);Console.Write(\" to \");Console.WriteLine(to);hannoi(n - 1, buffer, from, to);}}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 05c#语言基本元素概览,初始类型,变量与方法,算法简介