首页  >  文章  >  后端开发  >  C# 名称

C# 名称

WBOY
WBOY原创
2024-09-03 15:16:36828浏览

为了避免代码中出现硬编码字符串,我们在 C# 中使用了一个名为 nameof 运算符的运算符,它接受代码名称的元素并返回相同元素的字符串文字和类名,该类的所有成员都像变量、方法、常量可以作为参数传递给 nameof 运算符,并返回一个字符串文字,使用 nameof 运算符返回的这个字符串文字是一种特殊类型的字符串文字,因为编译器会检查给定的名称以查看是否存在某些内容通过该名称以及视觉工作室是否引用它。

C#中nameof运算符的语法如下:

nameof(Expression)

C# 中 nameof 运算符的工作

  • C# 中的 nameof 运算符经常被忽视,但它是 C# 中非常有用的运算符之一。
  • 它是一个运算符,没有它我们也可以进行编码,但为了在编译时指出某些错误,nameof 运算符非常重要。
  • 不同代码工件的名称由 C# 中的 nameof 运算符返回。
  • 考虑以下程序来演示 nameof 运算符在 C# 中的使用:

代码:

using System;
//a namespace called name is defined
namespace Name
{
//a class called check is defined
class Check
{
//main method is called
static void Main(string[] args)
{
//nameof operator is used
Console.WriteLine(nameof(Name));
Console.WriteLine(nameof(Check));
Console.WriteLine(nameof(Check.Main));
Console.WriteLine(nameof(Main));
Console.WriteLine(nameof(args));
}
}
}

输出:

C# 名称

在上面的程序中,定义了一个名为name的命名空间。然后定义一个名为check的类。然后调用main方法。然后 nameof 运算符用于标识命名空间的名称、程序的名称、main 方法及其参数。这里使用 nameof 运算符的要点是,如果代码名称的任何一项发生更改,那么我们必须更改代码中使用的所有 nameof 运算符,否则会发生构建失败。如果我们使用 nameof 运算符,那么编译器会指出错误,否则当程序在生产中使用时会发现这些错误。

C# nameof 示例

以下是下面提到的示例

示例#1

演示操作员名称的程序。

代码:

using System;
//a namespace called Features is defined
namespace Features
{
//a class called Example is defined
class Example
{
//an integer array called array is defined
int[] array = new int[5];
//main method is called
public static void Main(string[] args)
{
//an instance of the class Example is created
Example ex1 = new Example();
//try and catch block is defined
try
{
//the showop method is called using the instance of the Example class
ex1.showop(ex1.array);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
//the method that throws the exception is displayed
Console.WriteLine("The name of the method is: "+nameof(ex1.showop));
}
}
int showop(int[] x)
{
x[5] = 12;
return x[5];
}
}
}

输出:

C# 名称

在上面的程序中,定义了一个名为Features的命名空间。然后定义一个名为Example的类。然后定义一个整数数组,称为array。然后调用main方法。然后创建类Example 的实例。然后定义 try 和 catch 块。然后使用Example 类的实例调用showop 方法。然后显示抛出异常的方法。这里使用 nameof 运算符的要点是,如果代码名称的任何一项发生更改,那么我们必须更改代码中使用的所有 nameof 运算符,否则会发生构建失败。如果我们使用 nameof 运算符,那么编译器会指出错误,否则当程序在生产中使用时会发现这些错误。

示例#2

演示 nameof 运算符使用的程序。

代码:

using System;
//a class called check is defined
public class Check
{
//DateTime Function is used to find the current data and To LocalTime
private static DateTime Today = DateTime.Now;
//get and set functions are used
public string Name { get; set; }
//main method is called
public static void Main(string[] args)
{
//a variable called local_Time is defined to obtain the current date and To LocalTime
var local_Time = DateTime.Now.ToLocalTime();
//nameof operator is used to obtain the local_time, args, Systme information, main method, program name.
Console.WriteLine(nameof(local_Time));
Console.WriteLine(nameof(args));
Console.WriteLine(nameof(System.IO));
Console.WriteLine(nameof(Main));
Console.WriteLine(nameof(Check));
Console.WriteLine(nameof(Check.Today));
Console.WriteLine(nameof(Check.Name));
}
}

输出:

C# 名称

在上面的程序中,定义了一个名为Check的类。然后使用 DateTime 函数查找当前日期和本地时间。然后使用get和set函数。然后调用main方法。然后定义一个名为local_Time的变量来获取当前日期和LocalTime。然后使用nameof运算符获取local_time、args、系统信息、main方法、程序名称。这里使用 nameof 运算符的要点是,如果代码名称的任何一项发生更改,那么我们必须更改代码中使用的所有 nameof 运算符,否则会发生构建失败。如果我们使用 nameof 运算符,那么编译器会指出错误,否则当程序在生产中使用时会发现这些错误。

示例#3

演示操作员名称的程序。

代码:

using System;
//a class called check is defined
class Check
{
//a method called taste is defined
static void Taste(int argu)
{
//nameof operator is used
Console.WriteLine(nameof(argu));
Console.WriteLine(argu);
//nameof operator is used on the defined variable
var plane = "Shobha";
Console.WriteLine(nameof(plane));
}
//main method is called
static void Main()
{
//the method taste is called along with passing a parameter
Taste(200);
}
}

输出:

C# 名称

在上面的程序中,定义了一个名为check的类。然后在使用 nameof 运算符的类中定义一个名为taste 的方法。然后定义一个变量,再次使用 nameof 运算符。然后调用 main 方法,从该方法中调用定义的方法 Taste 并传递参数。这里使用 nameof 运算符的要点是,如果代码名称的任何一项发生更改,那么我们必须更改代码中使用的所有 nameof 运算符,否则会发生构建失败。如果我们使用 nameof 运算符,那么编译器会指出错误,否则当程序在生产中使用时会发现这些错误。

结论

在本教程中,我们通过定义了解 C# 中 nameof 运算符的概念、C# 中 nameof 运算符的语法、通过示例了解 C# 中 nameof 运算符的工作原理及其输出。

以上是C# 名称的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# Nullable下一篇:C# Buffer