EN CSharp Keyword static - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C#: static
Binds a member to a type instead of an instance. static class contains only static members; static lambda does not capture local variables.
Simple example (Example.cs)
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
System.Console.WriteLine(Tools.Double(4));
static class Tools { public static int Double(int x) => x * 2; }
Result:
8
Syntax
static class Tools { public static int Value { get; } }
Example 1: simple form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine(Tools.Double(6));
}
}
public static class Tools { public static int Double(int value) => value * 2; }
Example 2: another practical form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
int[] values = [1, 2, 3];
Console.WriteLine(values.Select(static value => value * value).Sum());
}
}
Official source: Microsoft Learn.