找到 34423 篇文章 关于编程

C#中的方法重载和歧义

George John
更新于 2020年6月21日 16:04:20

822 次浏览

通过方法重载,您可以在相同的范围内为同一个函数名提供多个定义。函数的定义必须通过参数列表中参数的类型和/或数量来区分。让我们来看一个例子。在这个例子中,调用将转到具有单个参数的方法 - 例子 using System; class Student {    static void DisplayMarks(int marks1 = 90) {       Console.WriteLine("具有一个参数的方法!");    }    static void DisplayMarks(int marks1, int marks2 = 95) {       Console.WriteLine("具有两个参数的方法!");    } ... 阅读更多

如何在C#中向ArrayList添加项?

Samual Sam
更新于 2020年6月21日 15:51:25

156 次浏览

ArrayList是C#中一种非泛型集合类型,它可以动态调整大小。让我们看看如何在C#中初始化ArrayList - ArrayList arr= new ArrayList();向ArrayList添加项 - ArrayList arr1 = new ArrayList(); arr1.Add(30); arr1.Add(70);让我们来看一个在C#中实现ArrayList的完整示例。这里我们有两个ArrayList。第二个ArrayList被添加到第一个列表中。例子 using System; using System.Collections; public class MyClass {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add(30);       arr1.Add(70);       ArrayList arr2 = ... 阅读更多

在C#中初始化HashSet

Ankith Reddy
更新于 2020年6月21日 15:50:18

742 次浏览

初始化HashSet。var h = new HashSet(arr1);上面,我们已在HashSet中设置了一个数组。以下是数组 - string[] arr1 = {    "electronics",    "accessories”,    "electronics", };以下是一个显示如何在C#中实现HashSet的示例 - 例子 using System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "electronics",          "accessories”,          "electronics",       };       Console.WriteLine(string.Join(",", arr1));       // HashSet       var h = new HashSet(arr1);       // 消除重复单词       string[] arr2 = h.ToArray();       Console.WriteLine(string.Join(",", arr2));    } }

c# 在以大写字母开头的单词之间添加空格

Arjun Thakur
更新于 2020年6月21日 15:52:44

4K+ 次浏览

要在以大写字母开头的单词之间添加空格,请尝试以下示例 - 首先,设置字符串。var str = "WelcomeToMyWebsite";如您在上面看到的,我们的字符串在大写字母前没有空格。要添加它,请使用LINQ,就像我们在下面所做的那样 - str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');以下是放置以大写字母开头的单词之间空格的完整代码 - 例子 using System; using System.Linq; class Demo {    static void Main() {       var str = "WelcomeToMyWebsite";       Console.WriteLine("原始字符串:" + str);     ... 阅读更多

反转C#中给定字符串中的单词

Chandu yadav
更新于 2020年6月21日 15:53:26

558 次浏览

假设以下为字符串 - Welcome反转字符串后,单词应显示为 - emocleW使用reverse()方法并尝试以下代码来反转字符串中的单词 - 例子 using System; using System.Linq; class Demo {    static void Main() {       string str = "Welcome";       // 反转字符串       string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));       Console.WriteLine(res);    } }

C#中DateTime的字符串格式

Samual Sam
更新于 2020年6月21日 15:54:17

378 次浏览

使用String.Format方法格式化DateTime。让我们来看一个例子 - 例子 using System; static class Demo {    static void Main() {       DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);       Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));       Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));       Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));    } }上面我们首先设置了DateTime类对象 - DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);为了格式化,我们使用了String.Format()方法并以不同的格式显示日期。String.Format("{0:y yy yyy yyyy}", d) String.Format("{0:M MM MMM MMMM}", d) String.Format("{0:d dd ddd dddd}", d)

C#中的序列化和反序列化

karthikeya Boyini
更新于 2020年6月21日 15:54:49

1K+ 次浏览

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

反转C#中的字符串

Chandu yadav
更新于 2020年6月21日 15:54:35

483 次浏览

要反转字符串,请使用Array.Reverse()方法。我们设置了一个方法并将字符串值作为“Henry”传递 - public static string ReverseFunc(string str) {    char[] ch = str.ToCharArray();    Array.Reverse(ch);    return new string(ch); }在上述方法中,我们将字符串转换为字符数组 - char[] ch = str.ToCharArray();然后使用Reverse()方法 - Array.Reverse(ch);

C#中的流和字节流

Samual Sam
更新于 2020年6月21日 15:55:37

2K+ 次浏览

文件是存储在磁盘上的具有特定名称和目录路径的数据集合。当打开文件进行读取或写入时,它就变成了一个流。流的类型包括 - 字节流 - 它包括Stream、FileStream、MemoryStream和BufferedStream。字符流 - 它包括Textreader-TextWriter、StreamReader、StraemWriter和其他流。字节流包含将流中的数据视为字节的类。Stream类是其他字节流类的基类。以下是属性 - CanRead - 流是否支持读取CanWrite - 流是否支持写入Length - 流的长度System.IO命名空间包含 ... 阅读更多

C#中字典和数组的区别是什么?

karthikeya Boyini
更新于 2020年6月21日 15:36:19

611 次浏览

字典字典是C#中键值对的集合。字典包含在System.Collection.Generics命名空间中。声明字典 - IDictionary d = new Dictionary();添加元素 - IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);数组数组存储相同类型元素的固定大小的顺序集合。它由连续的内存位置组成。最低地址对应于第一个元素,最高地址对应于最后一个元素。定义数组 - int[] arr = new int[5];初始化并将元素设置为数组。int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};

广告
© . All rights reserved.