第 5 节 C#语言基本元素概览,初识类型、变量与方法,算法简介

构成C#语言的基本元素

构成C#语言的基本元素为关键字操作符标识符标点符号文本、注释与空白,其中前5个元素称为 标记(Token) 即编译器能识别出来。

关键字(Keyword)

关键字是构成一门编程语言的基本词汇,下图是MSDN文档中关键字所在的目录

按逻辑分组

关键字

上下文关键字(在某个上下文语境中是关键字,离开这个语境便不是关键字)

操作符(Operator)

操作符也叫运算符,表达运算思想的符号。

标识符(Identifier)

标识符,通俗的讲即为取名字

1)什么是合法(编译器能读懂)的标识符
不能是关键字,非要用关键词命名必须在开头加“@”(@关键词);以字符(英文或者中文)或下划线(_)开头,不能以数字开头。

2)大小写规范
驼峰法(Camel):第一个单词的首字母小写,后面单词的首字母大写。例如,myVariable。
帕斯卡法(Pascal):构成标识符的每一个单词的首字母都大写。例如,MyVariable。
在C#语言里,变量名用Camel,方法名、类名、名称空间都用Pascal。
在Java语言里,方法名也用Camel。

3)命名规范:保证程序的可读性好
类:名词或名词复数
类的成员:属性:名词或名词复数;方法:动词或动词短语

4)如何阅读语言定义文档
语言文档的整体结构是以金字塔形式展示,先总体讲述,再一一分析。

标点符号

文本(字面值)

1)整数

int x = 2; //整型,32bit表示一个整数
long y = 3L; //长整型,64bit表示一个整数

2)实数

float x = 3.0F; //单精度浮点型,用32bit表示一个浮点数
double y = 4.00; //双精度浮点型,用64bit表示一个浮点数

3)字符

char c = 'a'; //字符类型的a

4)字符串

string str = "a"; //字符串类型的a

5)布尔

bool b = true;
bool b2 = false;

6)空(NULL)

string str = null;
Form f = null; //无引用

注释与空白

1)注释
单行://
多行(块注释): /* */
对选中内容进行注释:Ctrl+K,C
对选中内容取消注释:Ctrl+K,U
注意:块注释不能嵌套
2)空白
调整文档的格式:Ctrl+K,D;编辑–>高级–>设置文档的格式。

简要介绍类型、变量与方法

1)初识类型(Type)
类型亦称为数据类型(Data Type)
计算机识别数据类型,如下

var x = 3; //计算机自动识别数据类型,整型
Console.WriteLine(x.GetType().Name); //显示数据类型的名称

一般,在引入变量的时候就声明数据类型,如下

float x = 3.0F;

2)变量是存放数据的地方,简称“数据”。

int x; //变量的声明
x = 3; //变量的使用

3)方法(旧称函数)是处理数据的逻辑,又称“算法”,数据的加工厂。
三种常见的函数形式,有输入有输出,无输入有输出,有输入无输出。
有输入有输出

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int result = c.Add(3, 4); //函数Add的调用
            Console.WriteLine(result);
        }
    }

    class Calculator
    {
        //函数Add的声明
        public int Add(int a, int b) //public是为了在Calculator类的外部也能访问Add方法
        {
            int result = a + b;
            return result;
        }
    }
}

特殊地,有些函数不需要“原料”,也能返回一些数据;无返回值。
无输入有输出,有输入无输出

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample c = new Sample();
            c.PrintSum(3, 4);
        }
    }

    class Sample
    {
        public string Today() //无输入有输出
        {
            int day = DateTime.Now.Day;
            return day.ToString();
        }

        public void PrintSum(int a, int b) //有输入无输出,无返回值,要用void
        {
            int result = a + b;
            Console.WriteLine(result); //打出Console.WriteLine();的快捷方式:输入cw --> 双击tab键
        }
    }
}

4)程序 = 数据 + 算法
有了变量和方法就可以些有意义的程序了

算法简介

1)循环初体验

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample c = new Sample();
            c.PrintXTo1(10);
        }
    }

    class Sample
    {
        public void PrintXTo1(int x)
        {
            //循环
            for (int i = x; i > 0; i--) //输入for,双击tab键,得到for循环的主体
            {
                Console.WriteLine(i);
            }
        }
    }
}

2)递归初体验

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample c = new Sample();
            c.PrintXTo1(10);
        }
    }

    class Sample
    {
        public void PrintXTo1(int x)
        {
            //递归
            if (x == 1)
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine(x);
                PrintXTo1(x - 1);
            }
        }
    }
}

3)计算1到100的和
循环(占用内存较小)

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample c = new Sample();
            int result = c.SumFrom1ToX(100);
            Console.WriteLine(result);
        }
    }

    class Sample
    {
        public int SumFrom1ToX(int x)
        {
            //循环方法
            int result = 0;
            for (int i = 1; i < x + 1; i++) //输入for,双击tab键,得到for循环的主体
            {
                result += i;
            }
            return result;
        }
    }
}

递归(占用内存较大)

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

namespace PropertySample
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample c = new Sample();
            int result = c.SumFrom1ToX(100);
            Console.WriteLine(result);
        }
    }

    class Sample
    {
        //递归方法
        public int SumFrom1ToX(int x)
        {
            if (x == 1)
            {
                return 1;
            }
            else
            {
                int result = x + SumFrom1ToX(x - 1);
                return result;
            }
        }
    }
}

计算效率最高

public int SumFrom1ToX(int x)
{
    return (1 + x) * x / 2;
}

课后作业
“汉诺塔问题”
汉诺塔(又称河内塔)问题是印度的一个古老的传说。开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒,第一根上面套着64个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从这根棒搬到另一根棒上,规定可利用中间的一根棒作为帮助,但每次只能搬一片,而且大的不能放在小的上面。请问要搬动多少次才能把一根棒上的金片塔转移到另一根上?(提示:用递归方式思考和解决。)

步骤:
1)将n-1个圆盘放在暂存区
2)将最大的圆盘放在目标位置
3)再将暂存区视为原位置,原位置视为暂存区,将n-2个圆盘放在暂存区,最大圆盘放在目标位置,以此类推······

using System;

namespace Hanoi
{
    class Program
    {
        public const int MAX_VALUE = 30;//声明最大值常量
        public static int steps = 0; //移动次数
        static void Main(string[] args)
        {
            int levels = 0;
            Console.Write($"输入汉诺塔层数(1~{MAX_VALUE}): ");
            levels = int.Parse(Console.ReadLine());
            if (levels > 0 && levels < MAX_VALUE)
            {
                Move(levels, 'A', 'B', 'C');
                Console.WriteLine($"一共移动了{Program.steps}次。");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("输入范围错误");
            Console.ReadKey();
        }
        static void Move(int pile, char src, char tmp, char dst)
        {
            if (pile == 1)//圆盘数为1
            {
                Console.WriteLine($"{src} --> {dst}"); //$符号的用法
                steps++;
                return;
            }
            Move(pile - 1, src, dst, tmp); //圆盘移到暂存区B
            Move(1, src, tmp, dst); //输出具体步骤,并加1
            Move(pile - 1, tmp, src, dst); //圆盘移到目标位置C
        }
    }
}

其中,这里用到$符号,它的作用相当于String.Format()的简化。

string name = "liu";
int age = 10;

//复杂麻烦的写法
string str1 = "my name is " + name + ",my age is " + age + ".";
//使用Format的写法
string str2 = string.Format("my name is {0},my age is {1}.", name, age);
//使用$语法糖的写法
string str3 = $"my name is {name},my age is {age}.";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值