C#(CSharp)入门教程_c#教程

这篇C#入门教程涵盖了常量的声明与使用,转义字符的实践,包括单引号、双引号、换行和斜杠等,以及隐式和显示类型转换的示例。此外,还介绍了异常捕获的基础知识,确保程序在遇到错误时能够继续运行。最后,讲解了条件分支语句if和基本的循环语句在C#中的应用。
摘要由CSDN通过智能技术生成
        bool b1 = true;
        bool b2 = false;
        Console.WriteLine(b1 + "_" + b2);
        //char(字符)
        char ch = 'A';
        Console.WriteLine("ch的值为"+ch);

        //string(字符串)
        string str = "我要认认真真学CSharp";
        Console.WriteLine(str);



        #endregion

        int sbyteSize = sizeof(sbyte);
        Console.WriteLine("sbyte所占的字节数为:" + sbyteSize);
        int shortSize = sizeof(short);
        Console.WriteLine("short所占的字节数为:" + shortSize);
        int intSize = sizeof(int);
        Console.WriteLine("int所占的字节数为:" + intSize);
        int longSize = sizeof(long);
        Console.WriteLine("long所占的字节数为:" + longSize);
        Console.WriteLine("*********************************************");
        int byteSize = sizeof(byte);
        Console.WriteLine("byte所占的字节数为:" + byteSize);
        int uintSize = sizeof(uint);
        Console.WriteLine("uint所占的字节数为:" + uintSize);
        int ushortSize = sizeof(ushort);
        Console.WriteLine("ushort所占的字节数为:" + ushortSize);
        int ulongSize = sizeof(ulong);
        Console.WriteLine("ulong所占的字节数为:" + ulongSize);
        Console.WriteLine("*********************************************");
        int floatSize = sizeof(float);
        Console.WriteLine("float所占的字节数为:" + floatSize);
        int doubleSize = sizeof(double);
        Console.WriteLine("double所占的字节数为:" + doubleSize);
        int decimalSize = sizeof(decimal);
        Console.WriteLine("decimal所占的字节数为:" + decimalSize);
        Console.WriteLine("*********************************************");
        int boolSize = sizeof(bool);
        Console.WriteLine("bool所占的字节数为:" + boolSize);
        int charSize = sizeof(char);
        Console.WriteLine("char所占的字节数为:" + charSize);





    }
}

}


![](https://img-blog.csdnimg.cn/e0f23b21cbe84a63b49c62001aac294e.png)


## 常量



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

namespace CSharp入门_常量
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“常量”);
//常量使用关键字const
const int c = 1;
Console.WriteLine(“c的值为”+c);
//常量特点:必须初始化,不可以更改

        //使用常量的场景:声明一些常用的不变的量,如π

    }
}

}


![](https://img-blog.csdnimg.cn/fd1de7f120964a248575495879dd579e.png)


## 转义字符




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

namespace CSharp入门_转义字符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“转义字符”);
/*
* 转义字符的使用
*/
//单引号 ’
string str1 = “‘哈哈哈’”;
Console.WriteLine("str1 = "+str1);
//双引号 "
string str2 = ““CSharp太好学啦!””;
Console.WriteLine("str2 = " + str2);
//换行 \n
string str3 = ““CSharp\n太好学啦!””;
Console.WriteLine("str3 = " + str3);
//斜杠 \
string str4 = ““CSharp\太好学啦!””;
Console.WriteLine("str4 = " + str4);

        /*
         * 下面的是不太常用的转义字符
         * 
         */
        //制表符 \t (空一个tab键)
        string str5 = "我要好好学\t编程";
        Console.WriteLine("str5 = " + str5);
        //光标退格  \b  (光标退一格进行覆盖)
        string str6 = "我要好好学\b编程";
        Console.WriteLine("str6 = "
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值