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?
EDIT:
Edited to make it sound like real question.
This declaration uses 38 keywords:
[method: MethodImpl(MethodImplOptions.InternalCall)]
[SomeAttribute(typeof(object))]
[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, [param: 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.
= default(T)
. Oh, hmm, my compiler complains that you can't set a default value for a params
array : ( - mquander
MyGeneric<in T, out S>
for an extra one or two keywords (are different uses of out
different keywords?) - Richard
global
in there. global::System.Double
- Mark H
internal
to work. However, you could turn the attribute to say [method:MethodImpl(...)]
, though I'm not sure if method
is a keyword, it's highlighted as such in VS. - Mark H
T4 : enum
or I'm wrong - Andrzej Nosal
dynamic
args, can you not? - Mark H
protected
keyword...still compiles. - Brian Gideon
default
, dynamic
, method
, parameter
, and return
) - SLaks
checksum
, disable
, and restore
keywords? For #pragma
? - SLaks
params
? - SLaks
[garbage: MarshalAs(UnmanagedType.U4)]ulong h,
? - SLaks
partial
and extern
cannot be mixed :) - Andrzej Nosal
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 [1] 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)
[1] http://msdn.microsoft.com/en-us/library/x53a06bb.aspxobject a = null
. - Tomas Petricek
new static extern
for maximum coverage, I think, actually. Does new
count as a separate keyword in the type signature and constraint? - mquander
this
in there on the first argument then). - jalf
virtual
and override
on the same method -- and if you added either, you would have to remove the static
as well as the extern
, so it's a wash. You could add new
though. - Joe White
= default(T)
for another argument. default
hasn't been used yet. - jalf
in
to the generic parameter. - Joe White
in
for contravariance on delegates and interfaces. - Mark H
unsafe
. I'm not sure if that plays well with extern
, but you could always change static extern
to new virtual
. - Joe White
as
, is
, base
, null
, sizeof
, typeof
, and possibly a few others. - Joe White
(..., Action action = () => { /* go to town */ })
. Technically it's still part of the method declaration, but now you can write any statement -- so return
, switch
, checked
/unchecked
, fixed
, and a whole bunch else become fair game. - Joe White
return
as well - jalf
from
). But still. - Joe White
as
in a default. (It must be a compile-time constant) - SLaks
Default parameter value for 'pointless' must be a compile-time constant
- SLaks
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.