Use a Dictionary

RNA Transcription
RNA Transcription in C#
using System.Collections.Generic;
using System.Linq;

public static class RnaTranscription
{
    public static string ToRna(string dna) =>
        new(dna.Select(nucleotide => Complements[nucleotide]).ToArray());

    private static readonly Dictionary<char, char> Complements =
        new() { ['G'] = 'C', ['C'] = 'G', ['T'] = 'A', ['A'] = 'U' };
}

The first step is to decide how we want to iterate over and transform the nucleotides. As the string class implements the IEnumerable<char> interface, this allows us to call LINQ's Select() method on it, which can do both the iteration and the transformation in one go!

dna.Select(nucleotide => TODO)

Within the Select() method's lambda argument, we'll need to translate the nucleotide to its complement. We can do that by creating a dictionary with the key being the nucleotide and the value its complement:

private static readonly Dictionary<char, char> Complements = new Dictionary<char, char>
    {
        ['G'] = 'C',
        ['C'] = 'G',
        ['T'] = 'A',
        ['A'] = 'U'
    };

Using a target-typed new expression, we can omit the second Dictionary<char, char> type:

private static readonly Dictionary<char, char> Complements = new()
    {
        ['G'] = 'C',
        ['C'] = 'G',
        ['T'] = 'A',
        ['A'] = 'U'
    };

We can then use this dictionary in our lambda to get the nucleotide's complement:

dna.Select(nucleotide => Complements[nucleotide])

This will return a new IEnumerable<char>, which we can convert back to a string by first converting it to a char[] and then passing that to a string constructor:

public static string ToRna(string dna)
{
    return new string(dna.Select(nucleotide => Complements[nucleotide]).ToArray());
}

As this has just a single return statement, we can convert it to an [expression-bodied method][expression-bodied-method]:

public static string ToRna(string dna) =>
    new string(dna.Select(nucleotide => Complements[nucleotide]).ToArray());

Finally, we can once again use a target-typed new expression to replace new string with just new:

public static string ToRna(string dna) =>
    new(dna.Select(nucleotide => Complements[nucleotide]).ToArray());

And with that we have a concise, working implementation!

Alternative: using a foreach loop

You could also use a regular foreach loop instead of LINQ:

public static string ToRna(string dna)
{
    var rna = "";

    foreach (var nucleotide in dna)
        rna += Complements[nucleotide];

    return rna;
}
11th Dec 2024 · Found it useful?