14

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.

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • 10
    Those are not pointers. Those are references. – Sean Devlin May 06 '10 at 23:00
  • 6
    Pointers, maybe not. That's low-level memory management that a LOB developer often just doesn't need. *References*, on the other hand, is absolutely critical. – Rex M May 06 '10 at 23:01
  • I like this question. I might use it one day. +1 and favourited. :) – EMP May 06 '10 at 23:11
  • 3
    10 points for the brilliant message! if you cant do this then "bye-bye" – AJ. May 06 '10 at 23:18
  • 1
    @AJ - except if you can't do this, you won't get the message – womp May 06 '10 at 23:22
  • 5
    You should really be explicit in your example about whether `myObject` is a struct or a class, as this affects the output. Also, why call a type `myObject`? – Will Vousden May 06 '10 at 23:32
  • References are the same concept as pointers, just with pointer arithmetic removed and "->" replaced with "." (a needless syntactic difference). – Qwertie May 06 '10 at 23:42
  • 4
    As Will pointed out, the question is ambiguous. You're also implicitly assuming that myObject has a well-behaved Value property that won't return different values on subsequent gets. For all I know, the real output of the program is "I'm" "evil". – Dan Bryant May 06 '10 at 23:43
  • 1
    @Will Vousden: I thought the whole point of the question was to get the candidate to qualify their answer: "It depends. If myObject is a class then ..., but if it's a struct then ..." – EMP May 06 '10 at 23:45
  • Vacano uses the word "pointers" inside quotes. As if citing his coworkers. There are loads of people here intentionaly misinterpreting him. – Pedro Rolo May 07 '10 at 00:04
  • If you're looking for a job utilizing a programming language or platform, you should by that time understand common paradigms. Only exception I would hold would be for an intern. I'm primarily a C++ dev and if I see a candidate that doesn't understand something as simple as RAII (and likewise the Disposable pattern in .Net), something that simple and fundamental is a non-starter for a professional. – Nathan Ernst May 07 '10 at 00:08
  • 3
    Taking the incorrect interpretation, shouldn't the output by "bye", "hello" rather then "hello", "bye"? Regardless, I'd be concerned that the candidate just made a slip-up because he was brushing off the question rather then not understanding references. – Winston Ewert May 07 '10 at 00:08
  • 1
    I never found "brushing off" questions to be an effective interview technique. – spender May 07 '10 at 01:20
  • @Will Vousden: Good point. I did not post the full example. The full example had a better named class and it was clearly a class (not a struct) – Vaccano May 07 '10 at 03:42
  • See http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx for a discussion about the use of the word "reference" & "pointer". – Zaki May 08 '10 at 10:33

13 Answers13

18

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
  • 117,338
  • 33
  • 229
  • 351
16

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
  • 647,829
  • 179
  • 1,238
  • 2,067
10

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
  • 32,488
  • 9
  • 84
  • 95
5

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.

Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
  • Pointers are not that important anymore. I'd consider unsafe code in C# quite advanced. – dtb May 06 '10 at 23:02
  • 5
    @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 May 06 '10 at 23:08
  • There's an ocean between understanding pointers and programming with them. – spender May 06 '10 at 23:11
  • 3
    @Matt Greer: If someone could perfectly explain the difference between pass-by-value, pass-reference-by-value and pass-by-reference, then I'd hire him/her, even if he/she doesn't know what `1U << ((*(uint*)(&f) >> 23) - 126)` does. – dtb May 06 '10 at 23:15
  • @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 May 06 '10 at 23:16
  • 1
    @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 May 06 '10 at 23:21
  • 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 May 06 '10 at 23:22
  • @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 May 06 '10 at 23:33
  • "Consider a case where someone changes a reference to an object that's also in a Dictionary"... and Dictionary allows this? – spender May 06 '10 at 23:51
  • 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 May 07 '10 at 00:08
  • 1
    @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 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 May 07 '10 at 00:20
  • 1
    @Rubys: yeah that's why you don't put mutable data structures as keys in a dictionary. – Claudiu May 07 '10 at 00:38
  • ...but sadly, nothing to enforce it. – spender May 07 '10 at 00:45
  • 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 May 07 '10 at 23:49
  • @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 May 07 '10 at 23:55
4

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

Cory Petosky
  • 12,458
  • 3
  • 39
  • 44
  • lol. Good point. I should have used the correct "reference". – Vaccano May 07 '10 at 03:44
  • This answer has the clear intention to misinterpret the question and offend who made it. It doesn't add anything to the discussion. – Pedro Rolo Apr 02 '11 at 14:28
  • My comment looks meaner than intended. The question as originally asked by the OP (check the edit history) was significantly less clear, and it seemed a genuine misunderstanding of pointers vs. references. Please don't be so quick to judge something a year out of context. – Cory Petosky May 02 '11 at 21:24
3

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
  • 1,325
  • 6
  • 12
2

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
  • 9,882
  • 9
  • 34
  • 41
2

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
  • 6,655
  • 4
  • 30
  • 29
2

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
  • 3,167
  • 2
  • 25
  • 26
1

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
  • 73,184
  • 17
  • 117
  • 128
1

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.

MindingData
  • 11,924
  • 6
  • 49
  • 68
1

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
  • 12,848
  • 2
  • 29
  • 33
0

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
  • 2,510
  • 1
  • 22
  • 26