欢迎光临散文网 会员登陆 & 注册

三叶虫也能学会的C#零基础入门教程 01

2023-01-21 13:19 作者:LSPDFRCN-魔铠  | 我要投稿

https://b23.tv/vIYFnNC 1、运行首个C#程序 Console.WriteLine("Hello World!"); 语句是代码最小有意义单元,C#语句以分号作为结尾 Console表示控制台窗口类型 WriteLine()是Console类型的方法 方法:方法又叫做函数,此处的函数是Console.WriteLine(),意义是Console类型当中,有一个类型函数叫做Console.WriteLine() 类型:即class,现在可以认为它是一种抽象的概念;在代码中则是储存特定数据和组织类似操作结构 这里的这个类叫做Console,而Console.WriteLine()就是Console类的函数或者说方法 所谓参数是函数运行时所需要的变量,只要改变参数的输入,函数的输出结果就会随之发生改变 文本的string类型,与console类型一样,string类型也包括方法,string类型处理的是文本 2、声明和使用变量 所谓变量就是在程序运行中,其值可以发生改变的量 我们使用一个名字来定义一个抽象的东西,此后我们只要在代码中再用到这个名字,就是指的我们刚才定义的东西;我们只需要对它进行赋值,则此后用到它的时候,等价于用到我们赋的那个值 等号不是等于的意思,而是赋值的意思;相当于我们定义一个aFriend,它是某某 string aFriend = "某某"; Console.WriteLine(aFriend); 可见Console.WriteLine(aFriend)和Console.WriteLine("某某")是等价的,因为我们对aFriend这个变量进行了赋值,其值等于某某 string aFriend = "某某"; aFriend = "某某"; Console WriteLine(aFriend); 这个逻辑和我们刚才对aFriend赋值为某某是一样的,我们把aFriend值修改为某某,此时我们不再需要修改Console.WriteLine,因为我们的变量aFriend它的值已经发生变化,我们只需要继续打印这个结果,刚才的语句叫赋值语句 赋值语句有两种结构 第一种定义及赋值,对于我们尚未定义过的变量,我们必须对其先进行定义此时的语句结构是这样的:变量类型 变量名 赋值操作符 值 分号 语句结束 这里的string就是变量类型,这个类型叫做字符串,也就是文本字符 当我们对已经定义的变量进行赋值的时候,不需要再对它的类型进行定义,只需要对它直接进行赋值 string aFriend = "某某"; aFriend = "某某"; Console WriteLine("hello " + aFriend); 变量的值总是取最近的一次赋值,当我们代码从上到下运行的时候,最后的一个赋值将会是它最新的这个值 string aFriend = "某某"; aFriend = "某某"; Console WriteLine($"hello {aFriend} "); 3、使用字符串 string firstFriend = "某某"; string secondFriend = "某某"; Console WriteLine($"My friends are {firstFriend} and {secondFriend}"); Length是字符串的属性,可以返回字符串的字符数 1 string firstFriend = "某某"; 2 string secondFriend = "某某"; 3 Console WriteLine($"My friends are {firstFriend} and {secondFriend}"); 4 Console WriteLine($"The name {firstFriend} has {firstFriend.Length}"); 属性可以理解成是一个对象的一个值,或者是它的一个数据 1 string firstFriend = "某某"; 2 string secondFriend = "某某"; 3 Console WriteLine($"My friends are {firstFriend} and {secondFriend}"); 4 Console WriteLine($"The name {firstFriend} has {firstFriend.Length} letters."); 5 Console WriteLine($"The name {secondFriend} has {secondFriend.Length} letters."); 1 string greeting = " hello world! "; Console.WriteLine($"[{greeting}]"); 2 string trimmedGreeting = greeting.TrimStart(); Console.WriteLine($"[{trimmedGreeting}]); 3 trimmedGreeting = greeting.TrimEnd(); Console.WriteLine($"[{trimmedGreeting}]); 4 trimmedGreeting = greeting.Trim(); Console.WriteLine($"[{trimmedGreeting}]); 修改字符串的时候,并不是在原始的字符串上进行修改,即使我们最终修改完成以后,greeting并没有发生变化 当我们使用TrimStart(),TrimEnd()或者Trim()时,我们得到的是一个新的对象 string、greeting,这个greeting,我们把它称为对象 字符串里拥有各种属性方法 1 string sayHello = "Hello world"; 2 Console.WriteLine(sayHelllo); 3 sayHello = sayHello.Replace("Hello","Greeting"); 4 Console.WriteLine(sayHello); 4、发掘字符串的更多精彩用途 1 string songLyrics = "You say Goodbye,and I say hello"; 2 bool containsGoodbye = songLyrics.Contains("Goodbye"); Console.WriteLine("containsGoodbye"); 布尔值有两个:true,false 这个布尔类型和string不太一样的是它是被称为一个值类型的东西 值类型并不是类的对象

三叶虫也能学会的C#零基础入门教程 01的评论 (共 条)

分享到微博请遵守国家法律