This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Sunday, November 26, 2023

what is static keyword in c# with Example

 what is static keyword in c# with Example

Introduce static keyword in c#:

A static variable is declared with the help of static keyword. When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.


What is the uses of static keyword in C#?

The static keyword is used in C# to indicate that a member belongs to the type itself rather than to a specific object. This means that only one copy of the member exists, regardless of how many objects are created from the type


Example Of Static Keyword In C#:


// C# program to illustrate the

// concept of static variable

using System;


class Vehicle {


// Creating static variable

// Using static keyword

public static string Model_color = "Black";

}


// Driver Class

public class GFG {


// Main Method

static public void Main()

{


// Accessing the static variable

// using its class name

Console.WriteLine("Color of XY model is : {0} ",

Vehicle.Model_color);

}

}


No comments:

Post a Comment