views:

623

answers:

13

I asked a question like this in an interview for a entry level programmer:

var instance1 = new MyObject{Value = "hello"}
var instance2 = instance1;

instance1.Value = "bye";

Console.WriteLine(instance1.Value);
Console.WriteLine(instance2.Value);

The applicant responded with "hello", "bye" as the output.

Some of my co-workers said that "pointers" are not that important anymore or that this question is not a real judge of ability.

Are they right?

EDIT: The point was made that MyObject could have been a struct. That is a Good point. However, I did not post the full question I gave the interviewee. The full question had a class that was clearly a class (not a struct). It can be found here.

+17  A: 

They are absolutely wrong. Besides, this is a question about references, not pointers. If you don't get it, you don't deserve a paid job writing C#.

spender
I had a boss who had been in the game a long time, and I caught him explicitly passing C# containers by reference.
mikerobi
@mikerobi, what exactly is a "C# container"?
unforgiven3
@unforgiven3 I meant collection like ArayList.
mikerobi
ah, I see. thanks. makes sense.
unforgiven3
+11  A: 

This is really fundamental stuff. The question isn't even about pointers, it's about reference semantics in C#, which is one of the most important aspects of the language. Anyone who calls himself a C# programmer must understand this.

Will Vousden
+5  A: 

You should understand pointers and references long before you leave college.

I wrote the above before noticing that this question was tagged C#.

You don't need to know anything about pointers to program in C#. That said, I hold to my original statement in the general sense of being a professional programmer.

Besides, as others have said, this question is really about references. You REALLY have to understand references to be a C# programmer.

Charles
Pointers are not that important anymore. I'd consider unsafe code in C# quite advanced.
dtb
@dtb Pointers are fundamental to programming and how computers work, even if your language of choice lacks them. I would never even consider hiring anyone who doesn't understand pointers.
Matt Greer
There's an ocean between understanding pointers and programming with them.
spender
dtb
@dtb I must say that I find it worrying to hear this. Even if pointers are not used explicitly by the programmer, some important decisions could be made incorrectly due to not understanding what's going on behind the scenes.
Dan
@Dan: Such as? I can't imagine any situation while developing a C# program where someone who understands the difference between pass-by-value, pass-reference-by-value and pass-by-reference but has never programmed in C/C++ and doesn't know pointers could be bitten by this lack of knowledge.
dtb
Playing devils advocate, @Dan, from a C# perspective, can you be more explicit about the kinds of decision that might go awry? I'm not sure there are many, but at the same time, would look for understanding nonetheless.
spender
@dtb, Consider a case where someone changes a reference to an object that's also in a Dictionary. The change modifies the GetHashCode of that object, and it's "invisible" in the dictionary.
Rubys
@Rubys: I'm not sure I understand what you mean...
dtb
"Consider a case where someone changes a reference to an object that's also in a Dictionary"... and Dictionary allows this?
spender
There's an important difference between a reference and a pointer; with a reference, you can't be sure where it's pointing, because the location is subject to change. The magician pulled off his disappearing act. He can still pull the rabbit out of the hat when you need him to, but you shouldn't presume to know the details of his tricks. It's his job to show you the rabbit and you shouldn't have to know or care how he does it; after all, he might change the mechanism of the trick in the future. 'pointers' are already hiding plenty of details, like page faults and cache misses.
Dan Bryant
@spender, dtb: Dictionary has no way of preventing you from doing this. Imagine some class that has a single int field nameed B, and it implements GetHashCode using that field. If you declared var a = new MyObj(3) then inserted it into a <MyObj, bool> dictionary, and afterwards did a.B = 8; the dictionary's ContainsKey method, when called with a, will return false. Since the hash code is different now.
Rubys
Gotcha. There's a situation that never crossed my mind.
spender
@Rubys: yeah that's why you don't put mutable data structures as keys in a dictionary.
Claudiu
...but sadly, nothing to enforce it.
spender
Unless the key object raises an event when the hash code is changing, allowing the Dictionary to adjust itself. Obviously you need a better Dictionary class than the one provided by Microsoft in order to handle that case.
Ben Voigt
@dtb: I'd say it's very important to know that a .NET reference is a pointer that the garbage collector knows about, and the potential for trouble if you give a copy of that pointer to some native API that the garbage collector doesn't know about. Speaking of which, how can someone understand what the garbage collector is and does if they don't know basic memory organization?
Ben Voigt
+3  A: 

Knowing how object referencing works in C# I would deem to be important. (One can certainly survive a long way without knowing anything... but it's rarely OO code that you'd want to pay for)

Pointers on the other hand, not so much.

lzcd
+5  A: 

You should understand references before you start giving C# interviews.

Cory Petosky
lol. Good point. I should have used the correct "reference".
Vaccano
+1  A: 

Passing that question does not say much about somebody's ability, but failing it says very much about the person's lack of ability. Understanding reference vs. value semantics is fundamental to knowing how to program.

If a person has the wrong mental model about how the program will behave, at best he will be able to do "programming by accident". When things go wrong, he won't be able to find out why things went wrong.

Esko Luontola
+2  A: 

I think it's a fair question.

Be sure though that you don't simply grade this question on a simple PASS/FAIL basis. Follow up with questions like "Why?" and "What exactly does line 'instance1.Value = "bye"; ' or line 'var instance2 = instance1;' actually do?" Start a dialog about what's going on under the hood. You'll learn a great deal about a candidate by how deep they can carry the conversion as well as by whether they're able to follow your explanations.

C. Dragon 76
+2  A: 

If a candidate can't answer this question it shows great lack in their fundamental knowledge of C#. Show them the door and say bye bye!

Lee Treveil
+2  A: 

Even if you don't use pointers and unsafe code, you definitely should understand the concept and know how to use it. If you're gonna work in C#, I can forgive not knowing pointer arithmetic, because you most likely won't use it and most likely shouldn't use it. But pointers exist all around us even in the managed C# world.

string s = "abc"  
int a = 3;

One of these two variables, is actually a pointer (Reference. Whatever. Same thing). Which one? Honestly, A person who doesn't know that needs to go get a job using C for a year, after that he'll understand this for sure.

Imagine this bloke having to write a function that takes a ref string. It is:

  1. Immutable.
  2. A reference to an immutable object.
  3. A reference to a reference to an immutable object. (The immutability has nothing to do with the pointers. It will just be even more confusing if it's immutable ^^)

The man is going to go insane trying to figure that out if he didn't lose his pointer virginity in C beforehand. Or even worse, imagine some critical part of your code uses a Dictionary containing some valuable information in a class, and this guy changes one of it's values, which changes the GetHashValue of the object, and causes you to "lose" the instance.

Even if sane C# developers don't use explicit pointers in their code, that doesn't mean pointers are not used implicitly.

Every good programmer needs to understand exactly what does their language abstract away, otherwise he won't ever be able to understand the language properly. To do C# properly, you need to know C, and to do C properly, you need to know assembler.

Also, your question - For all you know, your interviewee assumed myObject is a struct. He most likely didn't, but still.
Edit: Yes, references are not fixed, unlike pointers. But conceptually there's really no difference.

Rubys
+14  A: 

Some of my co-workers said that "pointers" are not that important anymore

Understanding the difference between reference semantics and value semantics is crucial. It is fundamental to the design of the language and the type system. Understanding that references can be implemented with pointers is not particularly relevant for entry-level C# programmers; understanding the difference between copying by reference and copying by value is highly relevant.

or that this question is not a real judge of ability.

Well that depends on what ability you were attempting to test. If the ability to rapidly and accurately predict the behaviour of trivial C# programs is relevant to your job then I'd say that it is a good test of ability.

If the abilities to determine when there's not enough information given to solve the problem, and to ask the right questions to elicit that information, are relevant, then yes, this is a judge of relevant abilities. (A good candidate would ask to see the implementations of type myObject and member Value rather than assuming that myObject is a class and Value is a mutable property of type string.)

I say that all those abilities are relevant, and that this is a reasonable first question for an entry-level position.

Eric Lippert
+1  A: 

Honestly, I graduated from the full suit of Microsoft Courses about 2 months ago. Admittedly, I have been programming for a while longer than that, But I would say in C# I have been going for about 2 years.

I know for a fact, That these questions came up several times during my study, And it was absolutely, 100% critical that everyone in the class knew this. Infact I would think that the Microsoft Exam even has a few of these mixed in with it.

From a student's point of view (Albeit, a high level students point of view), everyone should know this. And if they don't then they do not know enough core programming (Or logic) to make it far in programming.

As a side note, My current job had a small test in the second interview. They made me write an app to test Prime Numbers, Factorials, Write my own LastIndexOf and one other that I can't remember off the top of my head.

I think logic is most important in programming. And more than knowing the C# language, This is more so a logic question.

Pyronaut
+1  A: 

This is not a good sign for anyone with any experience in C#.

Unless the candidate was just learning the language, and seemed very brilliant, it would be close to a deal-breaker for me.

At the minimum, a gaping hole in knowledge like this would tell me that much coaching would be required.

kyoryu
A: 

We have a list of technical questions that are asked of all developer candidates during the interview process. Of those questions a certain number must be answered correctly for the candidate to be considered, understanding C# references would be one of the questions.

Tim