Skip to content

Commit 342094d

Browse files
authored
static examples (#45642)
* Update access-modifiers.md Added description for file access type modifier * Update csrefKeywordsModifiers.cs Create examples for Local and Lamba expressions * Update static.md Reference new examples in static doc * Update csrefKeywordsModifiers.csproj Update Target Framework
1 parent 157858b commit 342094d

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

docs/csharp/language-reference/keywords/static.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ Use the `static` modifier to declare a static member, which belongs to the type
1717

1818
You can add the `static` modifier to a [local function](../../programming-guide/classes-and-structs/local-functions.md). A static local function can't capture local variables or instance state.
1919

20+
[!code-csharp[csrefKeywordsModifiers#28](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#28)]
21+
2022
You can add the `static` modifier to a [lambda expression](../operators/lambda-expressions.md) or [anonymous method](../operators/delegate-operator.md). A static lambda or anonymous method can't capture local variables or instance state.
2123

24+
[!code-csharp[csrefKeywordsModifiers#29](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#29)]
25+
2226
## Example - static class
2327

2428
The following class is declared as `static` and contains only `static` methods:

samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs

+42
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,46 @@ public static void Main(string[] args)
818818
}
819819
//</snippet27>
820820
}
821+
822+
//<snippet28>
823+
class Calc1
824+
{
825+
public void CalculateSum()
826+
{
827+
int a = 3;
828+
int b = 7;
829+
830+
// Static local function - cannot access 'a' or 'b' directly
831+
static int Add(int x, int y)
832+
{
833+
return x + y;
834+
}
835+
836+
int result = Add(a, b);
837+
Console.WriteLine($"Sum: {result}");
838+
}
839+
}
840+
/*
841+
Output:
842+
Sum: 10
843+
*/
844+
//</snippet28>
845+
846+
847+
//<snippet29>
848+
class Calc2
849+
{
850+
static void Main()
851+
{
852+
Func<int, int, int> add = static (a, b) => a + b;
853+
854+
int result = add(5, 10);
855+
Console.WriteLine($"Sum: {result}");
856+
}
857+
}
858+
/*
859+
Output:
860+
Sum: 15
861+
*/
862+
//</snippet29>
821863
}

samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net48</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<StartupObject>csrefKeywordsModifiers.UsingTest</StartupObject>
88
</PropertyGroup>

0 commit comments

Comments
 (0)