Skip to main content
15 votes

Tips for code-golfing in C#

In C#, we are not allowed to do if(n%2) to check if n is a even number. If we do, we get a ...
aloisdg's user avatar
  • 2,037
10 votes

Tips for code-golfing in C#

Avoid single-statement foreach loops If the loop's statement returns a non-int (including ...
the default.'s user avatar
  • 8,425
9 votes

Tips for code-golfing in C#

Use dynamic to group declarations dynamic is a forgotten feature that literally performs dynamic typing in C#! It has ...
the default.'s user avatar
  • 8,425
8 votes

Tips for code-golfing in C#

Use the weird kind of the is operator a is var b always defines the variable b equal to <...
the default.'s user avatar
  • 8,425
7 votes

Tips for code-golfing in C#

The Compute instance method of System.Data.DataTable, allows to evaluate a simple string expression, e.g. : C# (Visual C# ...
digEmAll's user avatar
  • 5,029
7 votes

Tips for code-golfing in C#

If you need to include multiple usings that all fall off of the same hierarchy it is often shorter to use the longest one as the ...
TheLethalCoder's user avatar
7 votes

Tips for code-golfing in C#

Swapping two variables Normally, to swap two variables, you have to declare a temporary variable to store the value. It would look like something along the lines of this: ...
Gymhgy's user avatar
  • 7,998
6 votes

Tips for code-golfing in C#

Use .NET 6 Top Level Statements, File-Scoped Namespaces, and target-typed new() Upgrade to .net 6 and you can use single file programs with no boilerplate. You can also use file-scoped namespaces like ...
José Mancharo's user avatar
6 votes

Tips for code-golfing in C#

Use C# lambda. Since PPCG allows lambda for input/output we should use them. A classic C# methods looks like this: ...
aloisdg's user avatar
  • 2,037
6 votes
Accepted

C# is the Main problem

C# Interactive Window Open the C# Interactive Window (View > Other Windows > C# Interactive in Visual Studio 2015). I suppose not all IDEs will have this. This approach executes C# in the ...
milk's user avatar
  • 3,163
6 votes
Accepted

Help golfing this solution further

string ... s="?,!;." ... var x=s.IndexOf(c); s is used only once, so could be inlined. ...
Peter Taylor's user avatar
  • 43.1k
4 votes

Tips for code-golfing in C#

Use Ranges and indices (C# 8) You can use the type Index, which can be used for indexing. You can create one from an int that counts from the beginning, or with a prefix ^ operator that counts from ...
aloisdg's user avatar
  • 2,037
4 votes

Tips for code-golfing in C#

If you're already using Linq in your answer and need to check for a none empty collection use Any(). Compare it to the following: ...
TheLethalCoder's user avatar
4 votes

Tips for code-golfing in C#

Use Action like Func to set a function to a variable. Action returns nothing (...
aloisdg's user avatar
  • 2,037
4 votes

Tips for code-golfing in C#

When you want to join something to output a string without delimiter, you should use string.Concat(), instead of ...
aloisdg's user avatar
  • 2,037
4 votes

Tips for code-golfing in C#

Use the one character non-short-circuiting variants of logical operators where possible: i>0||i<42 i>0|i<42 or <...
aloisdg's user avatar
  • 2,037
4 votes

Tips for code-golfing in C#

(A particular case of knowing your operator precedence!) Use % for tight-binding (somewhat) limited subtraction. This can save you a pair of parentheses around a ...
VisualMelon's user avatar
  • 3,888
4 votes

Tips for code-golfing in C#

Declare empty/matching strings together If you need to declare multiple empty/matching strings, you can save a few bytes with the following: ...
Erresen's user avatar
  • 459
4 votes

Tips for code-golfing in C#

C# Interactive Window AKA C# (Visual C# Interactive Compiler) on Try it Online This is a REPL for the C# language that includes many advantages to code golfing over using the traditional C# compiler....
dana's user avatar
  • 2,691
4 votes

Tips for code-golfing in C#

Group statements via tuples Let's assume the simplest case: you have two statements that return a value and would like to group them into one that returns the second value (perhaps the first one has ...
the default.'s user avatar
  • 8,425
4 votes

reach catch when everything in the try block is caught already

Warning: These programs are clone bombs (a sort of less-dangerous but still-dangerous form of a fork bomb); as such, do not run them on a production system without sandboxing or resource limits. Clone ...
3 votes

Tips for code-golfing in C#

Infinite Loops Default infinite loop provided in code golf is usually a while loop: while(true){...} This can be reduced (-1, as stated earlier) using an ...
Jeff Brophy's user avatar
3 votes

Tips for code-golfing in C#

Here are my 5 cents: You can plain replace Math.Max and Math.Min, the following way, to gain 6 bytes: ...
Zuabros's user avatar
  • 131
3 votes

Tips for code-golfing in C#

If you need to use an enum for a method it is often shorter to cast an int to it rather than using the value directly: ...
TheLethalCoder's user avatar
3 votes

Tips for code-golfing in C#

When to use a space and when you can remove it. After [] int[] f(char[] a){Console.Write('a');} ...
aloisdg's user avatar
  • 2,037
3 votes

Tips for code-golfing in C#

If you're already using Linq in your code and need to create a list use the following: var l=new int[0].ToList(); Compared to: ...
TheLethalCoder's user avatar
3 votes

Tips for code-golfing in C#

Always use the alias for a type if you need to type as they are usually shorter than the full names. It also means you don't need to include using System; or ...
TheLethalCoder's user avatar
3 votes

Tips for code-golfing in C#

If you want to return multiple values from a function, use C# 7 style tuples instead of out parameters: (int,int)d(int a,int b)=>(a/b,a%b);
user82593's user avatar
3 votes

Tips for code-golfing in C#

Convert a string to an IEnumerable<char> Common way would be to use .ToCharArray() (...
aloisdg's user avatar
  • 2,037
3 votes

Tips for code-golfing in C#

Use Range instead of Substring: var s="abcdef"; Console.Write(s.Substring(2,2)); // "cd" Console.Write(s[2..4]); // "cd" It doesn't ...
btnlq's user avatar
  • 111

Only top scored, non community-wiki answers of a minimum length are eligible