Back
Close

A Babel of Languages on CodinGame

TBali
23.3K views

C#

C#

Let's start our journey by visiting C#. It is not the most-used language as of today, but provides modern features, and is used widely in enterprise application development projects.

Checking the sample code

Let's see our sample puzzle solution in C#. It is a traditional, imperative programming style approach:

Looking at the syntax

What does the above code snippet reveal about C#?

  • using System; C# provides an extensive standard library, that is split to several namespaces. Here we will use only the very basics, but we still need to reference them.
  • class Solution C# is object-oriented. Like in Java, we need to have a class to hold our code, even if we don't really use OOP.
  • static void Main(string[] args) Every executable program needs a Main method, which will be called, when the program is started.
  • { C# has curly braces to construct a code block, similarly to C and many other languages. Whitespaces are usually have no meaning.
  • //... Here we commented out the reading from input stream, and replaced it with a simple assignment.
  • string m = "CG";
    • C# has a static and strong type system. m is declared as a string and it cannot change its type in this program.
    • Also note, that semicolon ; ends each statement.
  • string[] c = new string[2] {"00", "0"}; We have a wide range of available data structures. c is declared as an array (vector) holding 2 strings, and it is initialized with some string literals. Actually, there is also a shorter syntax for this line, but we are not codegolfing here... (Note: Codegolfing in C# is futile anyhow, as the language tends to be more verbose than others.)
  • var b = ""; Didn't we forget here declaring the type? No, because of type inference, the compiler can deduce from the assignment, that b must be a string.
  • foreach (char ch in m)
    • we can simply iterate through the characters of a string. foreach is available for all iterable data structures.
    • Also note, that the curly braces { and } can be omitted, because we use only a single statement within the loop.
  • b += Convert.ToString((int)ch, 2).PadLeft(7, '0'); Several things are happening in this line, so let's go one by one:
    • We can concatenate strings with the + operator. + is overloaded, meaning different things if operands are strings or integers.
    • There is a shortcut for the combination of an operator and an assignment (here: +=).
    • We can explicitly cast between compatible types: (int)ch simply gets the ascii code of the character.
    • Convert is a class in the standard library (remember the using System; line above?). Its ToString method can be referenced by dot .. In true OOP, even a simple string is an object. ToString() returns a string, so we can immediately invoke the PadLeft() method of the string class on the result.
    • While string literals use double quotes ", character literals are marked with single quotes '.
  • string ans = c[b[0] - (int)'0'] + " 0"; the only new thing in this line is referencing an array's elements using square brackets [ ]. Arrays are 0 indexed.
  • for (int i = 1; i < b.Length; i++) The typical C-style loop syntax. As a string is an object, we get its length by accessing its Length property, and not by a function call, as for example strlen() would do it in C.
  • if (b[i] == b[i - 1])
    • The conditional statement is quite straight-forward, using a comparison operator == in the boolean expression.
    • The ternary operator ? : could have been also used here, but I wanted to show the traditional if.
  • Console.WriteLine(ans); Writing to the console. We will need this a lot on CodinGame! :smiley:

Other characteristics

While not directly visible from the above code snippet, there are some other important aspects of C# worth noting:

  • C# is compiled to an intermediate representation, which will be run by a special virtual machine (Common Language Runtime). Such managed code provides lot of advantages, like preventing many types of coding bugs.
  • Just-In-Time compilation improves performance. C# code still runs slightly slower than a native compiled code (such as C++ or Rust), but the disadvantage is not huge, and it is still much faster than an interpreted language (such as Python or PHP).
  • C# is now cross-platform. Although developed by Microsoft, it is available not only for Windows. Also, it became an ECMA standard.
  • C# uses garbage collection, so you don't have to bother too much about memory management.
  • There are many advanced language features we could not address in this intro, such us generics, attributes, delegates, LINQ, etc.

Checking a different approach

C# is a multi-paradigm language. While its design is heavily OOP focused, you can use it in procedural or in functional style. As an illustration, let's revisit the Chuck Norris puzzle in a quite different way!
(Contributed by Djoums):

  • We can see the fluent interface in action here: Method calls can be chained to build a more complex query.
  • A Regex (regular expression) is a very concise and efficient way for string manipulation. Its disadvantage is that the resulting code is hard to read/understand without checking the regex pattern.
  • new string(...) Constructors are overloaded. Here, we create a string by repeating a character by a specified amount.
  • => A lambda expression is used to create an anonymous function that can be passed directly to a method as an argument.

For further study

Meme

Coming next...

After C#, let's check briefly another, quite similar language: Java!

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants