开发者

C# Generics - Strange Interview Question [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 10 years ago.

开发者_JS百科 Improve this question

An interviewer argued me "Genrics are not completely Genrics",

He provided the example (Parameters int k,int d are not generic)

public static void PrintThis<T>(T a, T b, T c, int k,int d)
{

}

He asked me if i prove still it is generics , i will be allowed to take up the next round. I did not know what he is expecting from me,and what he really means by showing such example.

Guide me how to smartly face such a strange interview ?.

Thanks in advance.


Some people are insane. You should smile and nod.


day... dreaming... at... computer....

"Did you give him the question?"

"Yes"

"Did he tell you it was BS?"

"No"

"Did you find him on StackOverflow asking how to answer the question?"

"Yes"

"Good, now we know. Get the next guy in here"

"Bwah... ha... ha..." repeat...


The fact that k and d are always int doesn't make the method any less generic, it just means that these parameters are always int regardless of the type of a, b & c.


Assuming that the interview question is a real one, I googled "Generics are not" + c#, and got these:

  • Generics are not C++ templates
  • generics are not just compiler magic
  • Generics are not covariant
  • Generics are not only a construct of the C# language; generics are defined with the CLR.
  • Generics are not limited to function definitions
  • Generics are not just used for collections
  • Generics are not limited to classes
  • C# does a kind of erasure but it's not as severe as Java's, and the reason is because generics are not a C#-specific feature, but rather a feature that must work across .NET, which supports multiple languages.

I got no matches at all for "Generics are not completely generic" or "Generics are not always generic".

So there. Have fun.


All a generic is is a parameterized type. I don't know what your interviewer is getting at. Does he claim that the method is not fully generic because not all of the method parameters have a generic type?

You are not required to use the same parameter type for all parameters, nor do all of the parameter types have to be generic.


Maybe he meant that a, b, and c must be of the same type, but that's stupid.


As far as I'm concerned, this is a generic method, by virtue of the fact that it has a type parameter. That not all parameters are of the generic type doesn't matter.


He asked me if i prove still it is generics , i will be allowed to take up the next round. I did not know what he is expecting from me,and what he really means by showing such example.

It sounds like this interviewer is looking for the answers given here - if your phrasing of his question captures his intent, then he's not arguing that generics aren't generic, he's asking whether you can intelligently discuss the "genericity" of the specified method.

As several others have noted, this is a generic method by virtue of the fact that it allows the user to specify a visible attribute of the function (in this case, the type of the first 3 parameters) at a later time. The fact that k and d are non-generic parameters would seem to simply be a red herring.

Being able to state that answer would more or less prove that you don't have a skewed notion of what "generic" means - in this case, a call-time specialization of a generic function.


Hmmm, late posting, but better late than never! :-)

Assuming your interviewer knows his stuff, this is really the sort of question that an interviewer asks you in order to do the following:

  1. Test your knowledge.
  2. Test you with a statement that he knows is incorrect to see how you will react to it, and how you will approach answering it.
  3. Engage you in a debate to get you to relax and talk freely about a subject that you hopefully have some familiarity with.

A good interviewer will achieve all three, while the not so good interviewer may simply be looking for ways to try and trap you because he's asked to interview too many candidates.

At the end of the day, if you don't know the answer or understand the question, you are better off saying so in the interview, and if you disagree with a statement, look at it as an opportunity to relax and engage in a discussion rather than simply answering questions during the interview.

And for what it's worth, the answer is that the method described IS a generic method, regardless of whether some parameters are of a fixed type or not. The proof is in the declaration itself where the generic syntax effectively instructs the compiler to make allowances for the generic arguments.


@Udana, could it be that the interviewer was trying to guage your response by asking you a question that has no meaning ?

I once had an interview in which the test question asked "what is the default margin size of a winform". I answered "I dont know and dont care, since i can set any value i want in the constructor"

At the end of the interview, the interviewer told me that the question was a trick to see who would guess and who would be honest when they didn't know something.


Taking the question literally:

  1. Show him you can call with T as string, int and say a Button.
  2. Show him the compiled generic code - I think its with ILDASM, you can see the generic method for reference types. You can also see the tuned versions of the generic method for value types, but I can't remember how. Maybe someone else can.


I would rather be interested in knowing the question he asked or the answer you gave just BEFORE this question was fired on you!!!.

Generally, such (weird/vague) questions are fired by interviewer if the interviewee shows discomfort in some topic, or gives some erratic answer. That forces the interviewer to take the interviewee down by cross questioning or asking surprising things.


hey its a good mind tickling question in generics. the interviewer just wanted to know your familiarization in generics. the used in method name is just to specify the type when you are calling the method. this may or may not be specified during the call. like

PrintThis("a","b", "c", int k,int d) // to specify <T> as string. 

this will be taken while compiling only. so the 3 parameters in which is specified will be of same type only.

PrintThis<string> ("a","b", "c", int k,int d) // explicitly specifying the type when you call the method. 

so by all means we just specify it is strongly typed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜