---
title: "Static keyword in C#"  
description: "The static keyword in C# is refering to something in the class, or the class itself, that is shared amongst all instances of the class.Static:A C#clas"  
author: "Anonymous User"  
published: 2011-01-01  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/86/static-keyword-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Static keyword in C#

The [static keyword](https://www.mindstick.com/forum/105398/what-is-the-static-keyword) in C# is refering to something in the class, or the class itself, that is shared amongst all [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-instances) of the class.

Static: A C#class can contain both static and non-static members. When we declare a memberwith the help of the keyword static, it becomes a static member. We access datamember without creating object.

Static Fields: Static field can be [declared as](https://www.mindstick.com/interview/2711/what-if-the-main-method-is-declared-as-private) follows by using thestatic keyword.

##### Example:

```
class myclass{       public static int x;       public static int y = 20;}
```

When we declare a static field inside a class, it can beinitialized with a value as shown above. All un-initialized static fieldsautomatically get initialized to their [default values](https://www.mindstick.com/blog/10881/default-values) when the class is loadedfirst time.

Static Member [Functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing): Inside a C# class,member functions can also be declared as static. But a static member functioncan access only other static members. They can [access non](https://www.mindstick.com/interview/2733/can-you-access-non-static-variable-in-static-context)-static members onlythrough an [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) of the class. We can invoke a static member only throughthe name of the class.

##### Example:

```
class myClass{       private staticint x = 20;       private static int y = 40;       public static void method()       {              Console.WriteLine("{0},{1}", x, y);       }} class myClsInit{       public static void main()       {              myClass.method();       }}
```

##### The output will be:

20, 40

---

Original Source: https://www.mindstick.com/blog/86/static-keyword-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
