Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

C# refresher

2 min read

Recently I changed work and am now employed at the SIAG, a local informatics company which operates mainly for the public administration sector. Today was my 3rd working day and I'm currently getting some intensive training for being able to join the project team as fast as possible. Since today I started to do some C# experiments for refreshing my knowledge, I thought that the following exercise (I did) may be interesting for someone of you. I - as a Java developer - had some difficulties initially, but the exercise is very useful for understanding how the inheritance etc. works in C#. The exercise is the following:
You have the following classes which MUST NOT be modified. In the main method of the class AgentSmith, produce the output "Never send a human to do a machine's job." by instantiating or invoking the available methods of the given classes.
Available classes:
using System;

public interface IWord {
void Print();
}

public interface IWord2 : IWord {
new void Print();
}

public abstract class Base {
protected static string msg = "send ";

public Base() {
Console.Write(this.GetString());
}

static Base() {
Console.Write("Never ");
}

public virtual void Print() {
Console.Write("to ");
}

protected virtual string GetString() {
return "llama ";
}
}

public class Derived : Base, IWord {

static Derived() {
Console.Write(Derived.msg);
}

public new virtual void Print() {
Console.Write("do ");
}

protected override string GetString() {
return "a ";
}
}

public sealed class MoreDerived : Derived, IWord {
public override void Print() {
Console.Write("mach");
}

void IWord.Print() {
Console.Write("a ");
}

protected override string GetString() {
return "do ";
}
}

public sealed class MoreDerived2 : Derived, IWord2 {

static MoreDerived2() {
Console.Write("ine");
}

public new void Print() {
Console.Write("job. ");
}

void IWord2.Print() {
Console.Write("job.");
}

protected override string GetString() {
return "'s ";
}
}

public abstract class Unfinished : Base {
protected new void Print() {
Console.Write("camel ");
}

protected override string GetString() {
return "human ";
}
}

public class Finished: Unfinished {
}
Put your solution inside the main method of the following class:
using System;

public class AgentSmith {
//Never send a human to do a machine's job.
public static void Main() {

//put your code here

Console.ReadLine();

}

}
Try it out, it's funny :)
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus