static keyword c# code example

Example 1: c# static meaning

In general, static means “associated with the class, not an instance”.
// Search c# static review for more detail

Example 2: static dictionary c#

/// <summary>
/// Contains plural logic in static class [separate file]
/// </summary>
static class PluralStatic
{
    /// <summary>
    /// Static string Dictionary example
    /// </summary>
    static Dictionary<string, string> _dict = new Dictionary<string, string>
    {
        {"entry", "entries"},
        {"image", "images"},
        {"view", "views"},
        {"file", "files"},
        {"result", "results"},
        {"word", "words"},
        {"definition", "definitions"},
        {"item", "items"},
        {"megabyte", "megabytes"},
        {"game", "games"}
    };

    /// <summary>
    /// Access the Dictionary from external sources
    /// </summary>
    public static string GetPlural(string word)
    {
        // Try to get the result in the static Dictionary
        string result;
        if (_dict.TryGetValue(word, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }
}

Example 3: static c#

(static) >> means that the method belongs to the Program class 
			and not an 'object' of the Program class.

Example 4: what is using static in c#

// The using static directive designates a type whose static 
// members and nested types you can access without specifying a type name.
using System;
using static System.Math;

public class Circle
{
   public Circle(double radius) 
   {
      Radius = radius;
   }
   public double Radius { get; set; }
   public double Diameter 
   {
      get { return 2 * Radius; }
   }
   public double Circumference 
   {
      get { return 2 * Radius * PI; }
      // otherwise if not using static "get { return 2 * Radius * Math.PI; }"
   }
   public double Area 
   {
      get { return PI * Pow(Radius, 2); }
     // otherwise if not using static "get { return Math.PI * Math.Pow(Radius, 2); }"
   }
}