C#
Basic Input/Output
Submission are either named functions, complete programs, or anonymous functions. Functions take input as parameters and generally return the result (sometimes ref
or out
parameters may be applicable). Programs take input either from 'argv' (command line arguments) or from 'STDIO' (Standard Input/Output).
These example solutions compute the factorial of an integer input. You can generally accept either the string representation of the integer (typical for a complete program) or an integer argument to a function or lambda expression.
// 'complete program' reading input from STDIN, writing output to STDOUT
static void Main()
{
int n=int.Parse(System.Console.ReadLine()), // read an integer
a=1; // accumulator
for(;n>0;)
a*=n--;
System.Console.WriteLine(a);
}
// 'complete program' reading input from command line arguments ('argv') and returning via exit code
static int Main(string[]args)
{
int n=int.Parse(args[0]), // read an integer
a=1; // accumulator
for(;n>0;)
a*=n--;
return a;
}
// recursive named function
int F(int n)
{
return n>1?F(n)*n:1;
}
// recursive named function (C# 6 'expression bodied' syntax)
int F(int n)=>n>1?F(n)*n:1;
// iterative anonymous typed lambda expression
(int n)=>
{
int a=1;
for(;n>0;n--)
a*=n;
return a;
}
// recursive named lambda expression (these are of no use in C# golfing)
System.Func<int,int>F=null;
F=n=>n>1?F(n)*n:1;
You may not have anonymous, untyped lambdas as you may see in other languages (e.g. x=>x*x
would have to be (int x)=>x*x
), as the type information can change the behaviour of the code, and as such is required to be unambiguous.
Version
C# has had about 2 breaking changes in its lifetime, and none of them have much bearing on golfing, so just use the latest: there are lots of potentially useful things in C# 6 and C# 7 to explore. Note that C# 7 tuples can't really be used at the moment because they require external libraries.
Libraries
Generally, everything you could possibly want to use is in mscorlib
, the library that is (by default) automatically included by the compiler (csc
(.NET) or mcs
(Mono)), so it is rare to need any external DLLs.
Note that using external DLLs requires compiler command-line arguments, and these must be added to your byte count.
csc /r:OxyPlot.dll main.cs
Some common .NET DLLs do not need these arguments when using csc
: http://stackoverflow.com/questions/2356180/no-need-to-reference-to-windows-forms-assembly
using
directives also count toward your byte count:
using System.LINQ;
using C=System.Console;
Truthy/Falsey
In C#/.NET, truthiness
and falseness
are defined by the true
and false
operators, which (unless you define them on your own classes) are only defined for the bool
data type.
If you are asked to produce a truth/falsy value then it must be something you could use in an if
statement, which would be the bool
values true
and false
, or a manually defined class (but this is unlikely ever to appear in code-golf). You can't, for example, return 1
for true and 0
for false, unless the challenge explicitly allows you to.
Try It Online
Remember that most people won't be running your code from an IDE, they will just Try It Online. TIO has Mono, and .NET CORE with C# 7.
In the past there have been some discrepancies between Mono and .NET compilation, notably with goto
statements, so it's always worth checking that your code runs as expected to avoid confusion. And everyone appreciates a Try It Online link in a good answer!