Static是C#中的一个关键字,使用该关键字无法实例化项目,这意味着使用static关键字的项目将变得不可实例化,并且它可以应用于类、变量、方法和构造函数,从而创建静态类、静态变量、静态方法、静态构造函数和静态成员。可以在成员的声明中使用关键字static来创建,不属于特定类型的对象;相反,它属于类型本身。
任何成员都可以通过在成员声明之前使用关键字 static 来声明为静态成员。让我们了解如何声明静态类、静态变量、静态方法和静态构造函数。
可以使用关键字 static 将类声明为静态类。静态类中只能包含静态数据成员、静态方法和静态构造函数。无法创建静态类的对象。静态类中不能进行继承。
让我们通过下面的程序来声明和理解静态类:
代码:
using System; //A static class is created using the keyword static static class example { // Creating a string variable and declaring it as static using static keyword public static string demo = "Welcome to C#"; } //Creating a public class public class check { // Calling the static main method static public void Main() { //The static member of the tutorial class which is static is called Console.WriteLine("Understanding static keyword : {0} ", example.demo); } }
上述程序的输出如下图所示:
输出:
可以使用关键字 static 将变量声明为静态变量。当我们将变量声明为静态变量并与所有类对象共享时,就会创建该变量的单个副本。我们使用类的名称来访问静态变量;不需要任何对象来访问静态变量。
让我们通过下面的程序来声明和理解静态变量:
代码:
using System; //A static class is created using the keyword static class check { // Creating a string variable and declaring it as static using static keyword public static string learn = "Python"; } //Creating a public class public class checkvar { // Calling the static main method static public void Main() { //The static member of the check class which is static is called Console.WriteLine("I want to learn : {0} ", check.learn); } }
上述程序的输出如下图所示:
输出:
可以使用关键字 static 将方法声明为静态方法。我们使用类的名称来访问静态方法;静态和非静态字段都可以使用静态方法访问。访问静态字段不需要对象或类名,而访问非静态字段则需要对象。
让我们通过下面的程序来声明和理解静态方法:
代码:
using System; //A static class is created using the keyword static class students { // Creating a string variable and declaring it as static using static keyword static public int number = 100; //Creating a method and declaring it as static using static keyword public static void count() { Console.WriteLine("Number of students"+ " learning python is :{0}", number); } } //Creating a public class public class display { // Calling the static main method static public void Main() { //class name is used to access the number of students students.count(); } }
上述程序的输出如下图所示:
输出:
可以使用关键字 static 将构造函数声明为静态构造函数。静态构造函数会在构造函数的实例运行之前自动调用,并且仅在程序中引用的类 id 之前的类中调用一次。类的名称必须与构造函数的名称相同。
让我们通过下面的程序来声明和理解静态构造函数:
代码:
using System; //A public class is created public class constructor { //A static constructor is defined whose name is same as the name of the class static constructor() { //This statement is displayed as the first line of the output and it is executed only once Console.WriteLine("Understanding Static Constructor"); } // The constructor instance is created public constructor(int a) { Console.WriteLine("constructor instance " + a); } // A public method is defined public string details(string name, string work) { return "Name: " + name + " Work: " + work; } // Calling the public main method public static void Main() { // Invoking all the constructors defined before constructor con = new constructor(1); Console.WriteLine(con.details("Shobha", "Data Scientist")); //Again Invoking all the constructors defined before constructor co = new constructor(2); Console.WriteLine(co.details("Shobha", "Tyson")); } }
上述程序的输出如下图所示:
输出:
静态类与非静态类类似,但静态类不能创建实例;也就是说,我们不能使用new运算符创建静态类的变量或对象,并且使用类名本身来访问静态类的成员。例如,考虑 .NET 框架中的静态类 System.math;它由执行数学运算所需的方法组成,无需创建数学类的实例。
C# 程序来说明静态类、静态变量、静态方法和静态构造函数。
代码:
using System; //A public static class is created public static class check { //A public static variable is created public static int height; //A static constructor is created static check() { // Method is called by the constructor height = distance(20); } //A static method is created static int distance(int number) { return Environment.TickCount * number; } } //A public class is created public class Program { public static void Main() { //Accessing the constructor and its object Console.WriteLine("Bird can fly upto: {0}", check.height); } }
上述程序的输出如下图所示:
输出:
以上是C# 中的静态关键字的详细内容。更多信息请关注PHP中文网其他相关文章!