找到 34423 篇文章 关于编程

如何在 C# 中捕获索引超出范围异常?

George John
更新于 2020-06-20 16:07:25

2K+ 浏览量

当您尝试访问索引超出数组边界的元素时,会发生 IndexOutOfRangeException。假设以下数组。它有 5 个元素 - int [] n = new int[5] {66, 33, 56, 23, 81};现在,如果您尝试访问索引大于 5 的元素,则会抛出 IndexOutOfRange 异常 - for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); }在上面的示例中,我们尝试访问索引 5 以上的元素,因此出现以下错误 - System.IndexOutOfRangeException: Index was outside the bounds of the array.这里 ... 阅读更多

如何在 C# 中捕获内存不足异常?

Ankith Reddy
更新于 2020-06-20 16:08:22

1K+ 浏览量

当 CLR 无法分配所需的足够内存时,会发生 System.OutOfMemoryException。System.OutOfMemoryException 继承自 System.SystemException 类。设置字符串 - string StudentName = "Tom"; string StudentSubject = "Maths";现在您需要使用分配的容量进行初始化,即初始值的长度 - StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length);现在,如果您尝试插入其他值,则会发生异常。sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1);出现以下异常 - System.OutOfMemoryException: Out of memory要捕获错误,请尝试以下代码 - 示例 实时演示using System; using System.Text; namespace Demo {    class Program {       static void ... 阅读更多

如何在 C# 中捕获文件未找到异常?

Arjun Thakur
更新于 2020-06-20 16:09:05

284 浏览量

当您尝试查找不存在的文件时,会引发文件未找到异常。假设我在 StreamReader 中设置了一个文件,“new.txt”,该文件不存在。如果您尝试使用 StreamReader(读取它)访问它,它将抛出 FileNotFoundException - using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); }要处理它,您需要使用 try 和 catch - Try {    using (StreamReader sReader = new StreamReader("new.txt")) {       sReader.ReadToEnd();    }    }catch (FileNotFoundException e) {       Console.WriteLine("File Not Found!");       Console.WriteLine(e);    }

C# 中的三角函数

Chandu yadav
更新于 2020-06-20 16:10:23

2K+ 浏览量

C# 中的三角函数包括 ACos、ASin、Sin、Cos、Tan 等。它属于 System 命名空间的 Math 类型。以下是一个示例,展示了如何在 C# 中实现三角函数 - 示例 实时演示using System; class Program {    static void Main() {       Console.WriteLine(Math.Acos(0));       Console.WriteLine(Math.Cos(2));       Console.WriteLine(Math.Asin(0.2));       Console.WriteLine(Math.Sin(2));       Console.WriteLine(Math.Atan(-5));       Console.WriteLine(Math.Tan(1));    } }输出1.5707963267949 -0.416146836547142 0.201357920790331 0.909297426825682 -1.37340076694502 1.5574077246549上面我们看到了使用 Asin 的反正弦值 - Math.Asin(0.2)同时,我们还看到了使用 Acos 的反余弦值 - Math.Acos(0)在 ... 阅读更多

什么是 C#.NET 中的序列化?

Samual Sam
更新于 2020-06-20 15:58:25

574 浏览量

序列化将对象转换为字节流,并将其转换为可以写入流的形式。这样做是为了将其保存到内存、文件或数据库中。序列化可以执行如下 - 二进制序列化所有成员,即使是只读成员,也会被序列化。XML 序列化它将对象的公共字段和属性序列化为符合特定 XML 架构定义语言文档的 XML 流。让我们看一个例子。首先设置流 - FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter();现在创建类的对象并调用具有三个参数的构造函数 - Employee ... 阅读更多

什么是 Regex 类及其在 C# 中的类方法?

Samual Sam
更新于 2020-06-20 16:00:18

179 浏览量

Regex 类用于表示正则表达式。正则表达式是可以与输入文本匹配的模式。以下是 Regex 类的使用方法 - 序号方法及说明1public bool IsMatch(string input)指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项。2public bool IsMatch(string input, int startat)指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中的指定起始位置开始。3public static bool IsMatch(string input, string pattern)指示指定的正则表达式是否在 ... 阅读更多

C# 中两个数组的交集

karthikeya Boyini
更新于 2020-06-20 16:01:36

5K+ 浏览量

要获取两个数组的交集,请使用 Intersect 方法。它是来自 System.Linq 命名空间的扩展方法。该方法返回两个数组之间的公共元素。首先设置两个数组 - int[] arr1 = { 44, 76, 98, 34 }; int[] arr2 = { 24, 98, 44, 55, 47, 86 };现在对这两个数组使用 Intersect - Arr1.Intersect(arr2);以下是完整代码 - 示例 实时演示using System; using System.Linq; class Program {    static void Main() {       int[] arr1 = { 44, 76, 98, 34 };       int[] arr2 = { 24, 98, 44, 55, 47, 86 };       var intersect = arr1.Intersect(arr2);       foreach (int res in intersect) {          Console.WriteLine(res);       }    } }输出44 98

如何在 C# 中显示数字的绝对值?

Arjun Thakur
更新于 2020-06-20 16:01:08

3K+ 浏览量

要在 C# 中查找数字的绝对值,请使用 Math.Abs 方法。首先设置数字 - int val1 = 77; int val2 = -88;现在取两个新变量并获取上述两个数字的绝对值 - int abs1 = Math.Abs(val1); int abs2 = Math.Abs(val2);让我们看看显示数字绝对值的完整代码 - 示例 实时演示using System; class Program {    static void Main() {       int val1 = 77;       int val2 = -88;       Console.WriteLine("Before...");       Console.WriteLine(val1);       Console.WriteLine(val2);       ... 阅读更多

C# 中的时间函数

Chandu yadav
更新于 2020-06-20 16:02:46

742 浏览量

DateTime 具有日期和时间的方法和属性,例如如何获取一天的小时数或分钟数等。让我们只关注时间函数 - 请参阅 MSDN(Microsoft Developer Network)以了解所有函数 - 序号。方法和属性1AddDays(Double)返回一个新的 DateTime,该 DateTime 将指定的天数添加到此实例的值。2AddHours(Double)返回一个新的 DateTime,该 DateTime 将指定的小时数添加到此实例的值。3AddMilliseconds(Double)返回一个新的 DateTime,该 DateTime 将指定的毫秒数添加到此实例的值。4AddMinutes(Double)返回一个新的 DateTime,该 DateTime 将指定 ... 阅读更多

IStructuralEquatable 接口在 C# 中的作用是什么?

Samual Sam
更新于 2020-06-20 16:03:02

299 浏览量

IStructuralEquatable 接口定义了支持对象结构比较的方法,这意味着两个对象因为具有相同的值而相等。它包含以下两种方法:序号方法及描述1Equals(Object,  IEqualityComparer)该方法确定一个对象是否在结构上等于当前实例。2GetHashCode(IEqualityComparer)该方法为当前实例生成一个哈希码。让我们来看一个创建 Tuple 对象并使用 IstructuralEquatable 接口的示例:创建 Tuple 对象 −var tupleOne = Tuple.Create(26.3, Double.NaN, 35.6); var tupleOne = Tuple.Create(26.3, Double.NaN, 35.6);现在使用默认比较器调用 IStructuralEquatable.Equals 检查相等性。IStructuralEquatable chk = tupleOne; Console.WriteLine(chk.Equals(tupleTwo, EqualityComparer.Default));阅读更多

广告

© . All rights reserved.