C# 使用静态

王林
发布: 2024-09-03 15:30:36
原创
618 人浏览过

using static 指令于 2016 年随着 C# 版本 6 的发布而引入,它允许我们引用静态成员,而无需命名空间引用,甚至不需要类型引用,并且 using static 指令也可用于引用嵌套类型。例如,通过使用静态指令,可以通过不引用类本身来引用控制台类的静态成员,这会产生非常简单而高效的代码,并且使用静态指令还使代码更具可读性,并且类的静态成员可以使用 static 指令导入到源文件中。

C# 中使用 static 指令的语法如下:

using static <fully-qualified-type-name>;
登录后复制

其中完全限定类型名称是类型名称,其静态且嵌套的成员可以被引用而无需使用类型本身。

 在 C# 中使用 static 指令的工作原理

  • 使用静态指令是在 2015 年随着 C# 版本 6 的发布而引入的。
  • 使用 static 指令可以引用静态成员,而无需引用命名空间或类。
  • 嵌套类型也可以通过使用 static 指令来引用。

考虑下面的示例来解释 C# 中使用 static 指令的用法:

using System.IO;
//using static directive is defined for system.Console
using static System.Console;
//a class called Check is defined
class Check
{
//Main method is called
static void Main()
{
//WriteLine method is referenced without using the Console class name
WriteLine("Welcome to using static directives");
}
}
登录后复制

输出:

C# 使用静态

在上面的程序中,为系统定义了使用静态指令。安慰。然后检查是否定义了类。然后调用main方法。然后,在不使用 Console 类名的情况下引用 WriteLine 方法,因为我们为系统使用了 static 指令。安慰。程序的输出如上面的快照所示。

C# 使用静态的示例

以下是下面提到的示例

示例#1

C# 程序演示在程序中使用 static 指令的用法

代码:

//using static directive for system.console
using static System.Console;
//using static directive for system.math
using static System.Math;
//using static directive for system.string
using static System.String;
//a namespace called Features is defined
namespace Features
{
//a class called import is defined
class Import
{
//main method is called
public static void Main(string[] args)
{
//the sqrt method is called without referencing math class because using static directive is used for system.math
double sqroot   = Sqrt(64);
//the concat method is called without referencing math class because using static directive is used for system.string
string newString = Concat("Learning"," is fun");
//the writeline method is called without referencing math class because using static directive is used for system.console
WriteLine(sqroot);
WriteLine(newString);
}
}
}
登录后复制

输出:

C# 使用静态

在上面的程序中,我们为 system.console 使用了 static 指令。然后我们为 system.math 使用了 static 指令。然后我们对 system.string 使用了 static 指令。然后定义一个名为 Features 的命名空间。然后定义一个名为 import 的类。然后调用main方法。然后调用 sqrt 方法而不引用数学类,因为 system.math 使用了 using static 指令。然后调用 concat 方法而不引用数学类,因为 using static 指令用于 system.string。然后调用 writeline 方法而不引用数学类,因为 using static 指令用于 system.console。程序的输出如上面的快照所示。

示例#2

C# 程序演示在程序中使用 static 指令的用法

代码:

using System;
//using static directive for system.console
using static System.Console;
//using static directive for system.string
using static System.String;
//a class called check is defined
class check
{
//main method is called
public static void Main(string[] args)
{
//the writeline method is called without referencing math class because using static directive is used for system.console
WriteLine("Check if the given number is positive or negative or zero:");
//a variable number is defined
int number = 10;
//Comparison operator is used to check if the number is greater than zero
if (number > 0)
//the writeline method is called without referencing math class because using static directive is used for system.console
WriteLine("Positive number");
//Comparison operator is used to check if the number is equal to zero
else if (number == 0)
//the writeline method is called without referencing math class because using static directive is used for system.console
WriteLine("Zero");
else
//the writeline method is called without referencing math class because using static directive is used for system.console
WriteLine("Negative number");
}
}
登录后复制

输出:

C# 使用静态

在上面的程序中,我们为 system.console 使用了 static 指令。然后我们对 system.string 使用了 static 指令。然后定义一个名为check的类。然后调用main方法。然后调用 writeline 方法而不引用数学类,因为 using static 指令用于 system.console。然后定义一个变量编号,将编号分配给该编号,需要检查该编号是正数、负数还是零。然后使用比较运算符检查数字是否大于零、小于零或等于零。程序的输出如上面的快照所示。

C# 使用静态的优点

有以下几个优点:

  • 如果类的成员被设为静态,则该静态成员不必依赖于该类的任何实例。如果未声明为静态,则这可以避免在调用成员时创建类的新实例。这再次需要垃圾收集,这是可以避免的。
  • 使用静态指令可以更轻松地实现实用方法,例如发送电子邮件、记录错误、从 Web 配置获取值等。
  • 使用静态指令时内存使用量较少,因为静态方法是共享的。

结论

在本教程中,我们通过定义了解在 C# 中使用 static 指令的概念、在 C# 中使用 static 指令的语法、通过编程示例及其输出来了解使用 static 指令的工作原理。

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

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!