找到 34423 篇文章,关于编程

C# 中的保留关键字是什么?

karthikeya Boyini
更新于 2020-06-20 10:25:34

3K+ 次浏览

关键字是预定义给 C# 编译器的保留字。这些关键字不能用作标识符。如果要将这些关键字用作标识符,可以在关键字前加 @ 字符。在 C# 中,某些标识符在代码上下文中具有特殊含义,例如 get 和 set 被称为上下文关键字。下表列出了保留关键字:abstract、as、base、bool、break、byte、case、catch、char、checked、class、const、continue、decimal、default、delegate、do、double、else、enum、event、explicit、extern、false、finally、fixed、float、for、foreach、goto、if、implicit、in(泛型修饰符)、int、interface、internal、is、lock、long、namespace、new、null、object、operator、out、out(泛型修饰符)、override、params、private、protected、public、readonly、ref、return、sbyte、sealed、short、sizeof、stackalloc、static、string、struct、switch、this、throw、true、try、typeof、uint、ulong、unchecked、unsafe、ushort、using、virtual、void、volatile、while、let。让我们来看一个在 C# 中使用 bool 保留关键字的示例:示例 在线演示 using System; using System.Collections; class Demo { static void Main() { bool[] arr = new bool[5]; ... 阅读更多

如何在 C# 中声明和实例化委托?

Samual Sam
更新于 2020-06-20 10:26:30

301 次浏览

C# 委托类似于 C 或 C++ 中的函数指针。委托是一个引用类型变量,它保存对方法的引用。该引用可以在运行时更改。声明委托的语法:delegate 现在让我们看看如何在 C# 中实例化委托。一旦声明了委托类型,就必须使用 new 关键字创建委托对象并将其与特定方法关联。创建委托时,传递给 new 表达式的参数的写法类似于方法调用,但没有方法的参数。public delegate void printString(string s); ... 阅读更多

如何在 C# 中声明和初始化常量字符串?

karthikeya Boyini
更新于 2020-06-20 10:27:16

10K+ 次浏览

要在 C# 中设置常量,请使用 const 关键字。初始化常量后,更改它会导致错误。让我们声明并初始化一个常量字符串:const string one= "Amit";现在您不能修改字符串 one,因为它被设置为常量。让我们来看一个包含三个常量字符串的示例。声明后,我们无法修改它:示例 在线演示 using System; class Demo { const string one= "Amit"; static void Main() { // 显示第一个常量字符串 Console.WriteLine(one); const string two = ... 阅读更多

如何在 C# 中声明和初始化列表?

Samual Sam
更新于 2020-06-20 10:28:04

12K+ 次浏览

要在 C# 中声明和初始化列表,首先声明列表:List myList = new List()现在添加元素:List myList = new List() { "one", "two", "three", };通过这种方式,我们在上面添加了六个元素。以下是 C# 中声明和初始化列表的完整代码:示例 在线演示 using System; using System.Collections.Generic; class Program { static void Main() { // 初始化集合 List myList = new List() { "one", "two", "three", "four", "five", "size" }; Console.WriteLine(myList.Count); } }输出6

如何在 C# 中声明和初始化字典?

karthikeya Boyini
更新于 2020-06-20 10:28:45

1K+ 次浏览

字典是 C# 中键值对的集合。字典包含在 System.Collection.Generics 命名空间中。声明和初始化字典:IDictionary d = new Dictionary();在上面,在声明字典对象时设置键和值的类型。int 是键的类型,string 是值的类型。两者都将存储在名为 d 的字典对象中。现在让我们来看一个示例:示例 在线演示 using System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary d = new Dictionary(); d.Add(1, 97); ... 阅读更多

如何在 C# 中声明事件?

Samual Sam
更新于 2020-06-20 10:29:25

302 次浏览

事件是用户操作,例如按键、点击、鼠标移动等,或某些事件,例如系统生成的通知。事件在类中声明和引发,并使用同一个类或其他类中的委托与事件处理程序关联。包含事件的类用于发布事件。要在类中声明事件,首先必须为事件声明委托类型。例如,public delegate string myDelegate(string str);现在,声明一个事件:event myDelegate newEvent;让我们来看一个在 C# 中使用事件的示例:示例 在线演示 using System; namespace Demo { ... 阅读更多

如何在 C# 中编写第一个程序?

karthikeya Boyini
更新于 2020-06-20 10:29:55

491 次浏览

以下是 C# 编程中的第一个程序:示例 在线演示 using System; namespace MyHelloWorldApplication { class MyDemoClass { static void Main(string[] args) { // 显示文本 Console.WriteLine("Hello World"); // 显示其他文本 Console.WriteLine("Welcome!"); Console.ReadKey(); } } }输出Hello World Welcome!现在让我们看看它包含什么。using System; - using 关键字用于在程序中包含 System 命名空间。命名空间声明 - 命名空间是... 阅读更多

C# 中的迭代器

Samual Sam
更新于 2020-06-20 10:30:53

307 次浏览

迭代器对集合执行自定义迭代。它使用 yield return 语句并一次返回一个元素。迭代器记住当前位置,在下次迭代中返回下一个元素。以下是一个示例:示例 在线演示 using System; using System.Collections.Generic; using System.Linq; namespace Demo { class Program { public static IEnumerable display() { int[] arr = new int[] {99, 45, 76}; foreach (var val in arr) { yield return val.ToString(); ... 阅读更多

C# 中的接口和继承

karthikeya Boyini
更新于 2020-06-20 10:11:42

2K+ 次浏览

接口接口定义为一种语法契约,所有继承该接口的类都应遵循该契约。接口定义了语法契约的“是什么”部分,而派生类定义了语法契约的“如何做”部分。让我们来看一个C#中接口的例子。示例 在线演示使用 System.Collections.Generic; using System.Linq; using System.Text; using System; namespace InterfaceApplication {    public interface ITransactions {       // 接口成员       void showTransaction();       double getAmount();    }    public class Transaction : ITransactions {       private string tCode;       ... 阅读更多

如何在C#中声明一个空字符串数组?

Samual Sam
更新于 2020年6月20日 10:12:28

2K+ 次浏览

在C#中,您可以使用字符串作为字符数组,但是,更常见的做法是使用string关键字来声明字符串变量。string关键字是System.String类的别名。要声明一个空字符串:string[] arr = new string[] {}; // 空字符串现在让我们看看打印这个空字符串会发生什么。示例 在线演示使用 System; namespace Demo {    class Program {       static void Main(string[] args) {          string[] arr = new string[] {}; // 空字符串         ... 阅读更多

广告
© . All rights reserved.