2K+ 阅读量
Java 中 System.exit(0) 的 C# 等价物是 - Environment.Exit(exitCode); Environment.Exit() 方法终止此进程并向操作系统返回退出代码。在上面,使用 exitCode 为 0(零)表示进程已成功完成。使用 exitCode 为非零数字表示错误,例如 - Environment.Exit(1) 返回值 1 表示您想要的文件不存在 Environment.Exit(2) 退出 返回值 2 表示文件格式不正确。
564 阅读量
要打开隐藏文件,首先需要将其显示出来。您可以通过删除设置在其上的隐藏属性来实现 -FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt"); file.Attributes &= ~FileAttributes.Hidden;现在将其视为普通文本文件并打开它。读取内容 -using (StreamReader sr = new StreamReader("myFile.txt")) { string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } }读取后,再次将属性设置为隐藏以隐藏文件 -file.Attributes |= FileAttributes.Hidden;
242 阅读量
这里,我们正在读取两个不同的文件 -读取文本文件 -示例 实时演示using System; using System.IO; namespace FileApplication { class Program { static void Main(string[] args) { try { using (StreamReader sr = new StreamReader("d:/new.txt")) { string line; // 读取并显示文件中的行,直到 // 文件末尾。 while ((line ... 阅读更多
250 阅读量
要打开纯文本文件,请使用 StreamReader 类。以下打开一个文件以进行读取 -StreamReader sr = new StreamReader("d:/new.txt")现在,显示文件的内容 -while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); }以下是代码 -示例 实时演示using System; using System.IO; namespace FileApplication { class Program { static void Main(string[] args) { try { using (StreamReader sr = new StreamReader("d:/new.txt")) { string line; // ... 阅读更多
60 阅读量
ToEven 属性与 MidpointRounding 枚举一起使用,用于将数字舍入到最接近的偶数。声明并初始化一个十进制数字 -decimal val = 70.45M;要将数字舍入到最接近的偶数 -decimal.Round(val, 0, MidpointRounding.ToEven)以下是完整代码 -示例 实时演示using System; using System.Linq; class Demo { static void Main() { decimal val = 70.45M; Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven)); } }输出70
301 阅读量
在 C# 中使用 Truncate 方法删除小数点后的所有数字。假设以下为我们的数字 -20.35M要删除小数点后的数字,请使用 Truncate() -decimal.Truncate(20.35M)让我们看看完整的代码 -示例using System; using System.Linq; class Demo { static void Main() { decimal dc = 20.35M; Console.WriteLine(dc.Truncate(val)); } }
77 阅读量
假设我们需要找到以下文件 -E:ew.txt要检查上述文件是否存在,请使用 Exists() 方法 -if (File.Exists(@"E:ew.txt")) { Console.WriteLine("文件存在..."); }以下是检查文件是否存在完整代码 -示例 实时演示using System; using System.IO; public class Demo { public static void Main() { if (File.Exists(@"E:ew.txt")) { Console.WriteLine("文件存在..."); } else { Console.WriteLine("E 目录中不存在该文件!"); } } }输出E 目录中不存在该文件!
1K+ 阅读量
垃圾回收器 (GC) 管理内存的分配和释放。垃圾回收器充当自动内存管理器。您无需了解如何分配和释放内存或管理使用该内存的对象的生命周期。每次使用“new”关键字声明对象或值类型装箱时,都会进行分配。分配通常非常快。当没有足够的内存来分配对象时,GC 必须收集并处理垃圾内存以腾出内存供新的分配使用。此过程称为 ... 阅读更多
嵌套类是在另一个封闭类中声明的类,它具有内部类和外部类。它是其封闭类的成员,封闭类的成员无法访问嵌套类的成员让我们看看 C# 中嵌套类的示例代码片段。这里,类 Two 是一个局部内部类 -示例class One { public int num1; public class Two { public int num2; } } class Demo { static void Main() { One x = new One(); ... 阅读更多
585 阅读量
继承使用继承,您可以指定新类应该继承现有类的成员。此现有类称为基类,新类称为派生类。继承实现 IS-A 关系。例如,哺乳动物 IS A 动物,狗 IS-A 哺乳动物,因此狗 IS-A 动物,依此类推。例如,基类 Shape 有派生类,如 Circle、Square、Rectangle 等。组合在组合下,如果父对象被删除,则子对象也会失去其状态。组合是聚合的一种特殊类型,并提供部分关系。例如, ... 阅读更多