C#入门版学习笔记

目录

编写第一个C#程序

C#中的命名空间

C#中的类 

程序的启动器-Main方法

Main方法的要求

Main方法的常见错误提示

​C#中的标识符

C#中的关键字

C#中的语句

C#中程序编写规范

C#中的变量

C#中变量的声明及初始化

C#中的数据类型

值类型和引用类型的区别

​ 一种特殊的字段-枚举

C#中的数据类型转换

C#中的拆箱和装箱

C#中的常量

C#中的表达式 

C #中的算术运算符

C#中的关系运算符

C#中的逻辑运算符 

C#中的位运算符 


编写第一个C#程序

打开Visual Studio工具,创建一个项目

打开Program.cs编写代码

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


namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 输出内容
            Console.WriteLine("人因梦想而伟大");
            Console.ReadLine();
        }
    }
}

 启动输出

C#中的命名空间

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

namespace Demo
{
    class Program
    {
        Test test = new Test();
        static void Main(string[] args)
        {
            // 输出内容
            Console.WriteLine("人因梦想而伟大");
            Console.ReadLine();
        }
    }
}

namespace Hello_Wrold
{
    class Test
    {

    }
}

C#中的类 

程序的启动器-Main方法

Main方法的要求

 基本要求

Main方法必须定义为static

Main的首字母必须大写

返回值可以是void或int

 命令行参数可选

 语法

static void Main (string[] args) {}

static void Main () {}

static int Main (string[] args) {}

static int Main () {}

Main方法的常见错误提示

错误一

 原因:

(1)没有Main方法

(2)Main方法写成了main方法,Main首字母应该大写

(3)没有static

错误二

 原因:

Main方法的返回值不是void或int

错误三

一个类里面有多个Main入口函数时需要指定

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

namespace Demo
{
    class Program
    {
        Test test = new Test();
        static int Main(string[] args)
        {
            // 输出内容
            Console.WriteLine("Hello World");
            Console.ReadLine();
            return 0;
        }
    }
}

namespace Hello_Wrold
{
    class Test
    {
        static int Main(string[] args)
        {
            // 输出内容
            Console.WriteLine("Hello World");
            Console.ReadLine();
            return 0;
        }
    }
}

 

C#中的标识符

C#中的关键字

intpublicthisfinallybooleanabstract
continuefloatlongshortthrowreturn
breakforforeachstaticnewinterface
ifgotodefaultbytedocase
voidtryswitchelsecatchprivate
doubleprotectedwhilecharclassusing

C#中的语句

概念:C#语句实质上就是实现各种功能代码的C#代码。

Hello World关键代码回顾:

Console.WriteLine("Hello World");
Console.ReadLine();

Console为控制台类

C#中程序编写规范

代码编写规则

  • 尽量使用借口编程
  • 关键语句一定编写注释
  • 局部变量随用随声明
  • 尽量少用goto语句
  • 如果参数多,建议使用结构
  • 避免对大段代码使用try...catch
  • 同一个文件中避免编写多个类
  • 字符串多变时,用StringBuilder
  • if语句块中使用"{}"
  • switch语句中一定编写default

两种命名方法

Pascal命名法:所有单词第一个字母大写,其他字母小写。

如:User   GetInfo

Camel命名法(驼峰命名法):除了第一个单词,所有单词第一个字母大写,其他字母小写。

如  userId   userName

C#中的变量

在C#中一个变量就是储存区(内存)中的一个存储单元。

C#中变量的声明及初始化

使用变量的步骤

  • 声明一个变量  根据类型分配空间
  • 初始化变量  将数据存入内存空间
  • 使用变量  取出数据使用

变量的声明

变量的命名规则 

变量的初始化 

变量的作用域 

变量使用常见错误

C#中的数据类型

类型描述范围默认值
bool布尔值True 或 FalseFalse
byte8 位无符号整数0 到 2550
char16 位 Unicode 字符U +0000 到 U +ffff'\0'
decimal128 位精确的十进制值,28-29 有效位数(-7.9 x 1028 到 7.9 x 1028) / 100 到 280.0M
double64 位双精度浮点型(+/-)5.0 x 10-324 到 (+/-)1.7 x 103080.0D
float32 位单精度浮点型-3.4 x 1038 到 + 3.4 x 10380.0F
int32 位有符号整数类型-2,147,483,648 到 2,147,483,6470
long64 位有符号整数类型-9,223,372,036,854,775,808 到 9,223,372,036,854,775,8070L
sbyte8 位有符号整数类型-128 到 1270
short16 位有符号整数类型-32,768 到 32,7670
uint32 位无符号整数类型0 到 4,294,967,2950
ulong64 位无符号整数类型0 到 18,446,744,073,709,551,6150
ushort16 位无符号整数类型0 到 65,5350

整数类型

浮点类型

 

bool类型 

引用类型

Object类 

 String类

 示例代码:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Consume c = new Consume();
            c.Record();
            Console.Read();
        }
    }

    class Card
    {
        public string Money
        {
            get;set;
        }
    }
    class Consume
    {
        public void Record()
        {
            Console.WriteLine("信用卡消费记录\n");
            Card pCard = new Card() { Money = "8000" };
            Console.WriteLine("信用卡的总额度:" + pCard.Money);
            Card sCard = pCard;
            sCard.Money = "3000";
            Console.WriteLine("信用卡消费记录:" + pCard.Money);
        }
    }
}

运行结果:

值类型和引用类型的区别

 一种特殊的字段-枚举

 示例代码:

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

namespace Demo
{
    public enum MRKJ
    {
        CS = 1,
        C = 2,
        Java = 3
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MRKJ.Java);
            Console.Read();
        }
    }
}

输出结果:

C#中的数据类型转换

隐式类型转换

示例代码:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            double score = 81.5;
            double score2 = score + 2;
            Console.WriteLine("第二次的成績:" + score2);
            Console.ReadLine();
        }
    }
}

 显式类型转换

(类型名)表达式

示例代码:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            double score = 81.5;
            int score2 = (int)(score + 2 );
            Console.WriteLine("第二次的成績:" + score2);
            Console.ReadLine();
        }
    }
}

Convert.To类型名(表达式)

示例代码:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            double score = 10.5;
            int c = Convert.ToInt32(score);
            Console.WriteLine("第二次的成績:" + c);
            Console.ReadLine();
        }
    }
}

C#中的拆箱和装箱

C#中的常量

 示例代码:

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

namespace Demo
{
    class Program
    {
        const int PRICE1 = PRICE2 * 2;
        const int PRICE2 = 368;

        static readonly int PRICE3 = PRICE4 * 2;
        static readonly int PRICE4 = 368;
        static void Main(string[] args)
        {
            Console.WriteLine("const常量輸出效果");
            Console.WriteLine("PRICE1的值:" + PRICE1);
            Console.WriteLine("PRICE2的值:" + PRICE2);
            Console.ReadLine();
            Console.WriteLine("readonly常量輸出效果");
            Console.WriteLine("PRICE3的值:" + PRICE3);
            Console.WriteLine("PRICE4的值:" + PRICE4);
            Console.ReadLine();
        }
    }
}

输出结果:

 常量和变量比较示例代码:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            const double PI = 3.14;
            double R;
            Console.WriteLine("請輸入圓的半徑:");
            R = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("圓的面積:" + PI * R * R);
            Console.ReadLine();
        }
    }
}

输出结果:

C#中的表达式 

C #中的算术运算符

 

C#中的关系运算符

C#中的逻辑运算符 

C#中的位运算符 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想天涯~路在脚下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值