Skip to main content

Static

  • Chapter
  • First Online:
Book cover C# 8 Quick Syntax Reference
  • 1511 Accesses

Abstract

The static keyword can be used to declare fields and methods that can be accessed without having to create an instance of the class. Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object. This means that static methods cannot use instance members since these methods are not part of an instance. On the other hand, instance methods can use both static and instance members.

class MyCircle {   // Instance variable (one per object)   public float r = 10F;   // Static/class variable (only one instance)   public static float pi = 3.14F;   // Instance method   public float GetArea()   {     return ComputeArea(r);   }   // Static/class method   public static float ComputeArea(float a)   {     return pi∗a∗a;   } }

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 29.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Mikael Olsson

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Olsson, M. (2020). Static. In: C# 8 Quick Syntax Reference. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5577-3_14

Download citation

Publish with us

Policies and ethics