College friend told me about interview question that is asked in one company:

Write reasonable(=compilable) method declaration that contains as many C# keywords as possible.

  • If there are 2 or more the same keywords they are counted as one.

  • Method's body is not considered.

C# has finite number of keywords so such method must exist. What is the code of this declaration?


Edited to make it sound like real question.


This declaration uses 38 keywords:

[method: MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern unsafe void MyMethod<T1, T2, T3>(
    this sbyte a, ref short b, out int c, long d, 
    byte e, ushort f, uint g, [parameter: MarshalAs(UnmanagedType.U4)]ulong h,
    float i, char j = default(char), bool k = false, global::System.Boolean l = true,
    double m = sizeof(int), decimal? n = null, dynamic o = null, params string[] z)
where T1 : struct where T2 : class where T3 : new();

It actually compiles, too.


As a thought exercise I came up with this, though I doubt it is definative:

protected internal new virtual unsafe void MethodName<T>(
    out int a, 
    ref int b,
    params int[] c) where T : class, new()

(Also not sure completely sure whether extern methods can be generic...)

Sounds like a completely nonsensical question to me though and I'm not sure what they were hoping to achieve by asking it.

Ah, looking that complete list of C# keywords all of the CLR types are considered to be keywords. Therefore assume the method above takes one parameter of each type - I'll be damned if I'm writing that out though...

Okay, based on the comments (I wish I hadn't started this now...)

protected internal static extern unsafe void MethodName<T, U>(
    out byte a, 
    ref char b,
    decimal c,
    double d,
    float e,
    long f,
    sbyte g,
    short h,
    uint i,
    dynamic j,
    bool k = true == false,
    T l = default(T),
    object m = null,
    int n = sizeof(ulong),
    params ushort[] o) where T : class, new()
                       where U : struct;

(Disappointingly had to loose a few things to get it to compile. Can't has a this parameter, because then the class must be static and static classes can't have protected members. Neither can we have the default values based on typeof(), is or as since they aren't compile time constants)


I quite like it, it shows knowledge of access modifiers, variable passing, types, generics and so on, it's probably going to make the interviewee think a bit, and it's unlikely somthing they'll have learned off the Internet just to pass the interview.


From a real-world approach, the question seems silly IMHO and certainly doesn't make good sense. I don't think a definite answer exists. My guess would be that the company was just probing to see how familiar the person is specifically with C# keywords and syntax.


I think the kind of discussion we can see in the top answers' comments is what the interviewer was looking for. As often, the interviewer was less interested in the final answer then in the process. Explaining why you couldn't use this keyword in your current declaration and how you can use that one shows that you understand (some of) the main concepts.


I think an interesting approach would be to write a program that from a list of keywords and rules, constructs the longest possible function declaration. This would demonstrate even more skill on the interviewee's part and if done correctly provide without doubt the best solution.