Quantcast
Channel: Tips for code-golfing in C# - Code Golf Stack Exchange
Browsing index pages (62 articles)
↧

Answer by romfir for Tips for code-golfing in C#

use (static) global include in csproj files:eg<ItemGroup><Using Include="System.Console" Static="True"/></ItemGroup>to change Console.Write to Writeand to skip using statements in the...

View Article


Answer by juanferrer for Tips for code-golfing in C#

Multiply by literal (e.g. 1f, 1d, 1m...) instead of casting:(decimal)a/bvs1m*a/b

View Article


Tips for code-golfing in C#

What general tips do you have for golfing in C#? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C# (e.g. "remove comments" is not an...

View Article

Answer by jcolebrand for Tips for code-golfing in C#

Instead of using .ToString() use +"" for numerics and other types that can be natively cast to a string safely..ToString() <-- 11 chars+""<-- 3 chars

View Article

Answer by jcolebrand for Tips for code-golfing in C#

Remember where private or public are inherent, such as the following:class Default{static void Main()as compared topublic class Default { public static void Main()

View Article


Answer by Joey for Tips for code-golfing in C#

Use var for declaring and initializing (single) variables to save characters on the type:string x="abc";becomesvar x="abc";Isn't particulaly necessary for int, of course.

View Article

Answer by Joey for Tips for code-golfing in C#

I once deliberately placed my program in namespace System so I can shorten access to a specific class. Compareusing System;using M=System.Math;tonamespace System{using M=Math;

View Article

Answer by jcolebrand for Tips for code-golfing in C#

Make classnames only one letter. Enhancing on Tips for code-golfing in C# we go from class Default{static void Main()toclass D{static void Main()which knocks out another 6 chars in this case.

View Article


Answer by jcolebrand for Tips for code-golfing in C#

Looping:Variable declarations:int max;for(int i=1;i<max;i++){}become:int max,i=1;for(;i<max;i++){}And if you have a need to or work with the i variable only once, you could start at -1 (or 0...

View Article


Answer by Nellius for Tips for code-golfing in C#

Favor the ternary operator over if..else blocks where appropriate.For example:if(i<1) j=1;else j=0;is more efficiently:j=i<1?1:0;

View Article

Answer by Nick Larsen for Tips for code-golfing in C#

Remember that the smallest compilable program in C# is 29 characters:class P{ static void Main() { }}So start by removing that from your length and judge your answer on how much over that it takes. C#...

View Article

Answer by Juliana Peña for Tips for code-golfing in C#

For one-line lambda expressions, you can skip the brackets and semicolon. For one-parameter expressions, you can skip the parentheses.Instead of...

View Article

Answer by Joey for Tips for code-golfing in C#

If using LINQ you can pass a method directly to Select instead of making a lambda.So, instead offoo.Select(x=>int.Parse(x))you can usefoo.Select(int.Parse)directly.(Discovered recently when...

View Article


Answer by Cristian Lupascu for Tips for code-golfing in C#

If you need to use Console.ReadLine() multiple times in your code (min 3 times), you could do:Func<string>r=Console.ReadLine;and then just user()instead

View Article

Answer by Cristian Lupascu for Tips for code-golfing in C#

If you need to use a generic Dictionary<TKey, TValue> at least two times in your code, you could declare a dictionary class, like in this example:class D:Dictionary<int,string>{}and then...

View Article


Answer by M_J_O_N_E_S for Tips for code-golfing in C#

There are circumstances when an output parameter can save characters. Here's a slightly contrived example, a 10 pin bowling score algorithm.With a return...

View Article

Answer by M_J_O_N_E_S for Tips for code-golfing in C#

Discovered tonight "in the trenches" while improving some golf code... if you have a class for your processing, you can do the work in the constructor to save declaring a method.I discovered this while...

View Article


Answer by Nathan Cooper for Tips for code-golfing in C#

Effective use of usingYou can replacefloat (which is an alias for System.Single) with z using z=System.Single;Then replace z=System.Single; with z=Single; by placing the program in the namespace...

View Article

Answer by EvilFonti for Tips for code-golfing in C#

Using LinqPad will give you the possibility to remove all the program overhead as you can execute statements directly. (And it should be fully legal in codegolf... No one says you need an .exe)Output...

View Article

Answer by maf-soft for Tips for code-golfing in C#

You can use the ternary operator to shorten complex if..else constructs even if you need to work on multiple or different variables in different branches.You can sometimes save some chars by doing all...

View Article
Browsing index pages (62 articles)


Latest Images