From cb53f802bb2f84271d6066894956673056f93d2a Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Wed, 17 Sep 2025 19:59:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#核心/C#核心.sln | 12 ++++ .../Lesson24_面向对象相关-StringBuilder.csproj | 11 +++ .../Program.cs | 71 +++++++++++++++++++ .../Lesson24_面向对象相关-StringBuilder练习.csproj | 11 +++ .../Program.cs | 26 +++++++ 5 files changed, 131 insertions(+) create mode 100644 C#核心/Lesson24_面向对象相关-StringBuilder/Lesson24_面向对象相关-StringBuilder.csproj create mode 100644 C#核心/Lesson24_面向对象相关-StringBuilder/Program.cs create mode 100644 C#核心/Lesson24_面向对象相关-StringBuilder练习/Lesson24_面向对象相关-StringBuilder练习.csproj create mode 100644 C#核心/Lesson24_面向对象相关-StringBuilder练习/Program.cs diff --git a/C#核心/C#核心.sln b/C#核心/C#核心.sln index 6810841..ce6fa64 100644 --- a/C#核心/C#核心.sln +++ b/C#核心/C#核心.sln @@ -57,6 +57,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关-String练习", "Lesson23_面向对象相关-String练习\Lesson23_面向对象相关-String练习.csproj", "{E35D8933-50D7-496A-8E4C-06DDD1CC0878}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson24_面向对象相关-StringBuilder", "Lesson24_面向对象相关-StringBuilder\Lesson24_面向对象相关-StringBuilder.csproj", "{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson24_面向对象相关-StringBuilder练习", "Lesson24_面向对象相关-StringBuilder练习\Lesson24_面向对象相关-StringBuilder练习.csproj", "{4FFD2030-9A14-4F59-A7B8-D218C551E489}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -171,6 +175,14 @@ Global {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Debug|Any CPU.Build.0 = Debug|Any CPU {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Release|Any CPU.ActiveCfg = Release|Any CPU {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Release|Any CPU.Build.0 = Release|Any CPU + {25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Release|Any CPU.Build.0 = Release|Any CPU + {4FFD2030-9A14-4F59-A7B8-D218C551E489}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4FFD2030-9A14-4F59-A7B8-D218C551E489}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4FFD2030-9A14-4F59-A7B8-D218C551E489}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4FFD2030-9A14-4F59-A7B8-D218C551E489}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#核心/Lesson24_面向对象相关-StringBuilder/Lesson24_面向对象相关-StringBuilder.csproj b/C#核心/Lesson24_面向对象相关-StringBuilder/Lesson24_面向对象相关-StringBuilder.csproj new file mode 100644 index 0000000..39630e7 --- /dev/null +++ b/C#核心/Lesson24_面向对象相关-StringBuilder/Lesson24_面向对象相关-StringBuilder.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson24_面向对象相关_StringBuilder + enable + enable + + + diff --git a/C#核心/Lesson24_面向对象相关-StringBuilder/Program.cs b/C#核心/Lesson24_面向对象相关-StringBuilder/Program.cs new file mode 100644 index 0000000..68f0b32 --- /dev/null +++ b/C#核心/Lesson24_面向对象相关-StringBuilder/Program.cs @@ -0,0 +1,71 @@ +using System.Text; + +namespace Lesson24_面向对象相关_StringBuilder +{ + internal class Program + { + static void Main(string[] args) + { + #region 知识回归 + //string是一个比较特殊的引用类型 + //每次赋值或修改,都会创建一个新的堆内存 + //如果频繁修改字符串,会产生大量垃圾对象,影响性能 + #endregion + + #region 知识点 StringBuilder + //C#提供的一个用于处理字符串的公共类 + //主要解决的问题是: + //修改字符串而不创建新的对象,需要频繁修改和拼接字符串可以使用他,可以提升性能 + //使用前 需要引用命名空间 + #endregion + + #region 初始化 直接指明内容 + StringBuilder str = new StringBuilder("Hello");//初始化时可以指定容量 eg:设置100的容量StringBuilder str = new StringBuilder("Hello",100); + Console.WriteLine(str); + #endregion + + #region 容量 + //StringBuilder存在一个容量的问题,每次往里添加内容时,如果超过了容量,就会自动扩容 + //获得容量 + Console.WriteLine(str.Capacity); + //获得字符长度 + #endregion + + #region 增删查改 + //增 Append + str.Append(",World"); + Console.WriteLine(str); + //增 AppendFormat + str.AppendFormat("! I am {0}, age {1}", "Tom", 20); + Console.WriteLine(str); + //删 Remove + str.Remove(5, 24);//从索引5开始删除6个字符 + Console.WriteLine(str); + //查 ToString + Console.WriteLine(str.ToString()); + //改 Replace + str[0] = 'h';//通过索引修改单个字符 + Console.WriteLine(str); + str.Replace("hello", "HELLO");//通过Replace修改字符串 + Console.WriteLine(str); + //插 Insert + str.Insert(5, ", C#");//在索引5处插入字符串 + Console.WriteLine(str); + //清空 Clear + str.Clear(); + Console.WriteLine(str); + + //stringbuilder的其他方法 + //Equals + if(str.Equals("hello")) + { + Console.WriteLine("相等"); + } + else + { + Console.WriteLine("不相等"); + } + #endregion + } + } +} diff --git a/C#核心/Lesson24_面向对象相关-StringBuilder练习/Lesson24_面向对象相关-StringBuilder练习.csproj b/C#核心/Lesson24_面向对象相关-StringBuilder练习/Lesson24_面向对象相关-StringBuilder练习.csproj new file mode 100644 index 0000000..0f2e882 --- /dev/null +++ b/C#核心/Lesson24_面向对象相关-StringBuilder练习/Lesson24_面向对象相关-StringBuilder练习.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson24_面向对象相关_StringBuilder练习 + enable + enable + + + diff --git a/C#核心/Lesson24_面向对象相关-StringBuilder练习/Program.cs b/C#核心/Lesson24_面向对象相关-StringBuilder练习/Program.cs new file mode 100644 index 0000000..96bb65c --- /dev/null +++ b/C#核心/Lesson24_面向对象相关-StringBuilder练习/Program.cs @@ -0,0 +1,26 @@ +namespace Lesson24_面向对象相关_StringBuilder练习 +{ + internal class Program + { + static void Main(string[] args) + { + #region 练习 + //1.请描述String和StringBuilder的区别 + //string比stringbuilder更容易产生垃圾 频繁修改会产生大量垃圾对象,影响性能 + //string比stringbuilder更加灵活 方法更多 + //如何选择string和stringbuilder + //需要频繁修改时,使用stringbuilder + //不需要频繁修改时或需要一些方法来处理麻烦的逻辑时,使用string + + + //2.如何优化内存 + //两个方面: + //如何节约 + //如何减少GC(垃圾回收) + //少new对象就少产生垃圾 + //合理使用static + //合理使用string和stringbuilder + #endregion + } + } +}