Some basic (but effective) .NET interview questions
I’ve been interviewing many .NET development developers lately and one of the most surprising things is that many candidates, both junior and senior level, cannot correctly answer questions which I consider very basic. I compile a short list of such questions below, hopefully it maybe helpful for you as interviewees or interviewers.
- What is the difference between
System.Int32type andinttype? I receive “interesting” answers like:System.Int32is reference type whileintis a value type, orSystem.Int32is presented in 4 bytes whileintonly needs 2 bytes. The correct answer is that there is no difference between them, they are alias. - What is the purpose of the method
GetHashCode()inSystem.Objectclass? Some candidates just cannot even guess what this method do. I’m not sure how they can properly use dictionary types without even knowing what the purpose of this method is. - Can you assign
nullvalue to a struct? Some confirm that they can (after I ask a few more questions to make sure that they are not referring to nullable types). - How does the modifier
readonlyaffect reference types? A few say that you cannot modify the fields of thereadonlyinstance. In fact, the only effect is that you cannot change the address inside that reference (i.e. assign it to another object). - Is this code valid
public const object KEY = new object()? Many, especially those with the Java background, say they do not see any problem with it. In fact, that code does not compile since, unlikereadonly, expressions assigned to “const” fields must be evaluatable at compile time. - Do you need to have a
catchblock whenever you use thetryblock? Some people just forget about thetry...finally...pattern. As a side note, I’ve seen a developer who used thecatchblock just to re-throw the caught exception in order to use thefinallyblock (since the poor guy thought that he could not usefinallywithout having thecatchblock). - How can you make a class serializable (for .NET Remoting)? Some thinks classes are made serializable by default. Of course they are not, you need to either annotate the class with the
Serializableattribute or implement theISerializableinterface. - What is the accessibility level of
internal protectedfields and methods? Some say these fields and methods can only be access by subclasses within the same assembly. Some others even tell me they do not think it is possible to combine these two modifiers. The right answer is these fields and methods can be accessed either by subclasses (in any assembly) or classes within the same assembly (or “friend” assembly).
That’s it. A few simple questions but can filter out quite a good number of candidates. I’ll talk about more interesting (and difficult) questions next time.
Addendum 4/29/2007: as some commenters thought that these are the only questions I use to assess the interviewees’ capability, I think I should make it clear that these questions are only found interesting by me since they are quite basic yet many candidates fail to answer many of them - and they are not the only measure I use to assess the candidates.
If a candidate can answer correctly all these questions, it does not necessarily mean that s/he would be hired; on the other hand, if a candidate cannot get all of these correct, it does not necessarily mean that s/he would not be hired - it only means s/he does not have full-score for one area assessed during the interview.
There are more areas to be assessed (e.g. design skills, problem-solving skills, analytical and logical thinking, communication skills, quick-learning ability, and team-fitness etc.) before the hire/no-hire decision can be made. The acceptance score for each area depends on the position (junior or senior) the candidate is applying for (but usually I use my instinct instead of basing on a frozen set of scores).



















Good work Buu Nguyen.
Keep watching http://www.DailyFreeCode.com very soon you will find lots of questions and lots more useful things.
Keep going good work.
Dotnetguts
http://www.dotnetguts.blogspot.com
Damn, got one half wrong (readonly; can only be assigned to once but forgot about members).
I contribute one more question:
- What is/are the difference(s) between String class and StringBuffer class? Almost candidates who claimed they are expert on Java/.NET failed on this question. They said that StringBuffer is more effective when handling with big-size strings (as the name suggested???). Some said StringBuffer has a richer set of public methods. And some even said that String is a subclass of StringBuffer. The correct answer is: String is immutable and StringBuffer is not. This can lead to another interesting question: so why immutable objects?
Anyway, I do not think those kinds of questions are good for a face-to-face interview. In DataDesign Vietnam, we have created a bunch of multiple-choice questions to test candidates in coding, database, object-oriented design, reading comprehension (much tougher than RC questions in TOEFL or TOEIC). We also use Zoho Challenge (http://zohochallenge.com/) to create various question papers for our first-round recruitment process.
Those questions are really good to measure knowledge and (to some extent) experience of a developer. One cannot answer them correctly without ever get her/his hands dirty on actual coding. They also test if the developer truly understands a given topic or not. However, the questions emphasize too much on knowledge. Some smart developers may fail miserably on those questions, yet some dumb developers may pass luckily.
One very crucial question I always ask is the difference between an interface and an abstract class. I consider this a fundamental OOP concept.
Another good one is to define boxing, and whether it’s good or bad and how to avoid it.
It’s my opinion that trivia questions won’t always get you the kind of candidate you want. I know many very intelligent developers, who write the most beautiful code I’ve ever seen, who don’t stuff their brain with trivia answers because the IDE can tell them while they are coding with intellisense.
At the least, if you plan to ask trivia questions, you should do it in a more useful way during a face-to-face interview. For example, you could ask “Why might you use the readonly keyword when defining a class data member?” or “Give an example of a case where you would want to make an object serializable and explain why.”
Too often I’ve gone into an interview and been asked trivia or brain teasers which just end up making me feel stupid; it always leaves me with a bad taste in my mouth about the engineer who is interviewing me. I don’t want to work for someone who thinks answers to trivia questions are the key to writing good code.
Hello,
Very good list and very interesting.
I have prepared SQL Server questions in the same way. If anybody interested can visit : SQL Server Interview Questions and Answers Complete List Download
I haven’t read through most of your questions yet…. but the first one is already wrong. In a 64-bit environment, int would be int64.
@Nam:
Thanks for your addition, string-vs-StringBuffer and the-need-for-immutative are very good questions.
I totally agree with you that the questions in this list are not good enough for a face-to-face interviews. These questions are only used to assess whether people have a good understanding about the basics of the language / framework and I mostly use them during the phone screening interview. In addition, these questions are not everything I ask during the phone interviews though.
Interesting! I’ll take a look at that. Thanks for introducing.
@jayson:
Thank you. Interface-vs-abstract class is a very good OO design question, especially when the candidate has to provide reasons when one (interface or abstract class) is favored than another. The box/un-box is definitely a good basic question to ask.
@jglover:
I can understand your point. As I answered to Nam, these questions are not meant to select good candidates, they are meant to filter out bad candidates. After all, what do you expect from a .NET candidate who does not know to properly use dictionary types, when to use
readonlyandconst, properly code the accessibility level of fields / methods, how to prepare an object for serialization, and how to write propertry...catch...finallyblock and so on…?On the other hand, to select good developers you would need to do much more work. For example, you would want to ask more “deep” and “open” questions like “Do you think C# and VB.NET assemblies are always interoperable? And why?” or “What is your most favorite refactoring technique [or design pattern]? And why?”. These questions allow the candidates to express their thoughts, and that will help you pick the better out of the others. I’ll post these questions in another entry.
@Pinal Dave:
Thanks, I’ve added the link provided to my to-read list.
@SomeProgrammer:
That’s not true. In fact, if you compile the below piece of code with the compiler option to target x-64 (64-bit) platforms
static void Main() { int i = 10; }The generated IL will clearly indicate that
iis a 32-bit integer.method private hidebysig static void Main() cil managed { .entrypoint // Code size 5 (0x5) .maxstack 1 .locals init ([0] int32 i) IL_0000: nop IL_0001: ldc.i4.s 10 IL_0003: stloc.0 IL_0004: ret } // end of method Program::MainIn addition, the C# ECMA specification (4th edition, section 8.2.1) explicitly says that
intis a 32-bit signed integral type (just aslongis 64-bit and so forth). And that’s reasonable for the same mapping rules to be used for all platforms, otherwise you need to care about the target platform for such trivial stuff in order to write proper code.More please.
I really don’t like that questions. I’m not interested in knowing if the candidate knows about much of those things. I know they are important, but I like people who can learn, and can learn fast.
Knowing if a const is assignable or not, like already said, the VS removes any doubt, but knowing some keys design patterns and how apply them, Having good communication skills, and don’t being ass old are more important “skills” to me.
Ex: If a person don’t know about the benefits of StringBuilder over String, but can understand the difference quickly when you explain to him, it’s very important.
Most of so called seniors programmers love himself to much to accept new things. I won’t hire them, even they answer all the question I could ask
@Nico:
Thanks for your comments. I have added an Addendum to the entry to clarify the fact that I do not use these questions alone to assess a candidate.
For the quick learning thing that you mentioned, I’d prefer the pick the quick learners who can answer these basic questions than those who cannot - so that they can use their quick learning ability to do more useful things during their job (than, say, learn how to serialize an object). The point is, you don’t want to hire a quick leaner with little or no knowledge and then have him/her start learning about it after the hire.
On the other hand, you’re of course right that there are many other important things to be assessed like communication skills, team fitness, design knowledge etc.
Ahh.. ok! Thanks for the heads-up!
I’ve been doing .NET programming for the last 2 years. I feel like if someone asked me these questions out of the blue, I would most likely fail it. I guess I’m a horrible programmer, or something.
@SomeProgrammer
I guess I have not made myself clear enough on the role of these questions in my original post as well as my several comments - I have updated the Addendum. In short, this is just one knowledge area to be assessed, and it comprises just a portion of what makes up a good programmer. So there’s no point in thinking s/o is a horrible programmer if s/he cannot get full-score for this portion :-).
This came just in time! I’m off for an interview next week and these were a solid starting point for me. The majority seem to be C# related whereas you can expect different questions for a VB.NET position (please don’t throw anything at me).
“How can you make a class serializable? Some thinks classes are made serializable by default. Of course they are not…”
I’m not sure I agree with that statement. I’m certainly open to correction here, but I have written SEVERAL different classes that are NOT marked with the Serializable attribute, that I can pass to an XmlSerializer class, and it WILL serialize them to XML (and/or binary for that matter). In fact, I have more than one working pieces of code right now where it *does* serialize the object to XML that gets written to disk. Could you please elaborate on this?
I understand that if the desire is to “explicitly” mark a class as Serializable, that attribute and others can be applied. But it’s certainly not necessary in order to be able to serialize an object/class.
@Yex:
That’s an interesting point, thanks for pointing it out! You are completely right, we do not need to mark a class as Serializable or have it implement ISerializable in order to use the XmlSerializer (but not others). However, I consider there are 2 distinct built-in serialization mechanisms in .NET.
1) BinaryFormatter/SoapFormatter (require classes to be annotated with SerializableAttribute or implement ISerializable)
2) XmlSerializer (does not care about SerializableAttribute or ISerializable).
The differences between these two categories are
1) Only public fields or fields with public assessor can be serialized using an XmlSerializer. On the other hand, both public and non-public fields of a class can be serialized by BinaryFormatter/SoapFormatter
2) There can be no circular reference within the object graph to be serialized via the XmlSerializer. There’s no such restriction with BinaryFormatter/SoapFormatter
3) The XmlSerializer cannot serialize interface references (e.g. IList) within the to-be-serialized object, while BinaryFormatter/SoapFormatter does not care
4) In order for instances of a class to be serialized by .NET Remoting, it must be annotated with Serializable attribute or implement ISerializable (this is a derived reason, because .NET Remoting uses BinaryFormatter/SoapFormatter internally [if you implement a custom formatter, it’s another story then])
5) …and several more…
IMO, the reasons behind all the limitations of XmlSerializer can be attributed to two things: performance and interoperability – but the elaboration of these should be another entry’s topic. Back to my interview question, I really aimed as the first mechanism, i.e. BinaryFormatter/SoapFormatter and .NET Remoting-able and that’s why I wrote that the SerializableAttribute / ISerializable is required. My bad for not making that clear - I will reword the question to make this point clearer.
But then, as we have known the differences between the two, an interesting question comes to mind is - why does the former mechanism requires classes to be explicitly marked as serializable while the latter does not? I guess it is partly due to the fact that by allowing the serialization of non-public data, BinaryFormatter/SoapFormatter break encapsulation. In fact, given class A, which is serializable, if clients of A serializes A (e.g. for storage or .NET Remoting purposes), then if the internals of A change (e.g. change private field’s name or type), then all the clients will break when trying to deserialize the old binary/XML stream. In addition, since BinaryFormatter/SoapFormatter bypass the object’s constructors when deserializing it, the object’ invariants enforced via these constructors can be broken by some malicious code (again, this is too involved to be discussed here). Given these problems, it’s reasonable for MS to decide (just as Sun with Java) that classes must be explicitly marked as serializable for them to be used by BinaryFormatter/SoapFormatter.
Nice questions. Just want to add few more:
- What is the difference between Trace class and Debug class?
- In debugging process, can you change a variable at run time? If yes, how?
Data structure questions for the implementation in C# (from easy to medium) :
- public string Merge(string one, string two, boolean oneFirst) {…}
for example: the result of Merge(”abc”,”cd”, true) : acbdc
the result of Merge(”abc”,”cd”, false) : cadbc
- public void PrintBinary(int num) {…}
- public int CalDiffer(int[] arr, int size)
// calculate the difference between the smallest odd number and the largest even number
- public void mirror(Node head)
// Binary search tree, mirror the tree so that if the tree has left node is 2 right node is 3 than after the mirror, right node is 2 and left node is 3
- Post order and Inorder nonrecursive
Have fun.
@Be’ Ba:
Thanks for your additions.
Hi bro,
I found that after 1/2 year doing BA, I totally failed in case i am an interviewee. No way back for me being a developer.
Hix
Here are my answers:
#1: I don’t know any difference between Int32 and Int. I am used to declare an ‘int’ in my code for integer variable.
#2: I don’t know what the purposes of GetHashCode() method are. In reality, I have never cared that method when using Dictionary type. Working with method of dictionary type I just glance on its short description in MSDN.
#3: I didn’t know a ’struct’ cannot afford a null value until I got a compiling error.
#4: I supposed that when placing a ‘readonly’ word before a variable I would make it unchangeable.
#5: I didn’t know I would get a compiling error writing this code ‘public const object KEY = new object()’
#6: I do know try, catch, finally block but I have never used a try without a catch. I don’t even know it is compliable.
#7: I didn’t know the [serializable] until I got runtime error when dealing with .NET remoting.
#8: I have never used an explicit internal modifier in my variable.
==============================================
Face it! How many developers around you who are doing well their job but cannot get 8 correct answers?
What are issues/potential issues of my code since I didn’t know such basic knowledge? How possibilities are they? How their severity will be? Do they impact seriously to application performance? Are they hard to prevent? Are they difficult to find during compile time/unit testing stage?
We don’t even need such basic knowledge to be good developer.
How many percent of bugs on a software product that was caused by lacking of language proficiency? How many projects which were fail because of the same root cause?
Is that possible to avoid such bugs if we have a good language-proficient one in 10-member team who does review code periodically?
Excluding controlling/system programming or some kind of programming that needs very efficient using CPU/memory, most software bug was caused by lacking of business domain. And the most dangerous and latent bugs are come from inefficient strategic thinking implemented.
What makes a good software developer?
Is that the one who has a high proficiency in programming language with an innate ability to create well-crafted code (code that is simple, extensible, optimized and robust)? Is that the one who is software craftsman?
I don’t think those are mandatory for a good developer.
Why don’t we focus on questioning to find out ability of strategic thinking or problem awareness rather than language proficiency?
=================================================
Instead of 8 questions I think 3 is enough and save time for checking strategic thinking or problem awareness:
#1: What are common issues of developers when working with a dictionary type?
#2: How does the modifier ‘readonly’ affect reference types?
If we define: readonly ClassA classA; Is this possible to change classA.Name property? Why?
#3: Can you give some scenarios that you use with try, catch, finally and then explain them?
What are your thoughts?
Thanks for your comment, wsw.
If the candidate does not know the purpose of GetHashCode(), I do not see any value in asking this question. Same argument for Question #3. Question #2 is same as mine thus no comment :-). For your responses to the 8 questions, by presenting various contexts, I can beat them one by one, but then I’d rather provide a more generic response below.
“Good” is context-dependent (organization issues, application constraints, and design objectives etc.), in my case, even if the candidate can answer all of these questions, s/he is not yet considered “good”, and s/he is asked with many other questions. On the other hand, somebody who does not know the implication of serialization (e.g. breaking encapsulation) and readonly keyword, the differences between struct and class, how to clean up resource without having to do a unnecessary-catch (e.g. catch and throw the same thing), and how to limit the accessibility level of fields/methods… is immediately considered not good enough.
You may not have to think about these stuffs in many applications, but when you are designing, say, a class library to be used by many other applications, then you would definitely need to know about them. Fine, some candidates maybe super smart people and can learn the in’s and out’s of .NET and OO very shortly, but then, these developers are not very common and you do not know about it until they have finished with the “learning”. And since I do not want to make any bet in hiring, I want a person who is both smart and knowledgeable. Luckily, I did find some, although I had to fail a lot more.
Finally, if you do not see the application of such basic (fundamental is probably a better word) knowledge in your projects, then it’s totally fine - it’s totally context-dependent.
Thanks again and hope to hear from you.
I think the first question is wrong
- “System.Int32″ is a struct with some useful methods with int type. In C# struct is similar to class.
- “int” just a type
For example you can call System.Int32.Parse(string s) and can not call int.Parse
This is a basic difference
I don’t like your question because it is so detail and deep in technical. In my opinion with this question you will skip many good candidate who don’t like remember small detail and knowledge can be find easily when they work with computer. I think if you don’t want skip good candidate you should concentrate the methodology, OOP, data structure, design database question (I meant solving problem question, not technical question).
You should remember the better people don’t like remember detail things.
Very funny, thanhcccp. I like your sense of humor. I skip the rest of your comments though.
[…] and ‘internal’ accessibility levels” and so on (I listed some others here) are theoretical and not useful for the job. They went on saying that they never found themselves […]
I fail your .NET test, but I’ve executed many projects with a positive attitude, passion for my job, problem solving skills, and a good work ethic that calls for working nights and weekends to meet a deadline. Look for candidates who strive for continuous improvement, passion for development or creating things, and good problem solving skills and you should do alright in your hiring.
I found a good link having all type Microsoft.net interview questions and answers.
http://www.dotnetinterviewfaqs.com
found some good questions at
http://www.dotnetcurry.com/BrowseArticles.aspx?CatID=58
Check this one.
http://dotnetinterview.googlepages.com/
[…] the last few posts on interviewing, I discussed mostly about the technical aspects of the interviewing process […]
Alex George said:
[quote]
I found a good link having all type Microsoft.net interview questions and answers.
http://www.dotnetinterviewfaqs.com
[/quote]
Due to the underline of the link, I read that dotnetinterviewFAGS. Thanks a lot Firefox!
Here are more .Net Interview Questions
http://oopsconcepts.com/interviewquestions/
These types of question make you end up with developers who sit and read all day long when they should be getting work done. I have drilled through canidates for these types of questions and they have gotten every single one of them right down to the answer on the page, but could not implement any of them. Examples of previous work and talking about specific projects they have “Finished” always seems to have a better outcome for me.
@Brian:
Asking such questions is among the many things you do in an interview to assess technical competence. Yes, you need to look at their past projects and contributions. And you definitely need to ask them to write the code… To avoid hiring the wrong people, none the above should be skipped. And in my experience of skipping no step, people who are answer these types of questions are often more technically capable than those who can’t.
Finally, it’s true that some people can simply cram over things the day before the interview without really understanding - but a good interviewer should be able to probe until s/he is confidence that the candidate really knows what s/he is talking about.
“What is the accessibility level of internal protected fields[….]”
So you like to ask questions about things that no one would bother to do?
If i came across this in code, I’d find who wrote it and smack them upside the head. Use “protected” or “internal” - each has its use, so pick one.
Just because C# and the Visual Studio compiler lets you get away with something because someone missed what should throw an error doesn’t show anything about the skills of a programmer.
My answer to your question would be “Don’t you have work to get done? Why are you wasting your time on useless trivia like this?”
The point is to hire someone who can write code that meets specs, is easy to read and maintain, and gets done, correctly, by deadline. Asking them this sort of thing isn’t a sign of anything other than they’re a geek - and in my hiring experience, geeks are the least like likely to write code that’s easy to read and maintain or make deadlines.
@Wink Jr.:
Thanks for your comments. I can confirm that there’s perfect reason for the combination of internal and protected keywords, and the “VS compiler”, if it exists at all :-), would have no reason to warn the programmers about that usage. I also have seen many people who could answer such questions in interview and could write very good code.
I do understand where you’re coming from though. If you are happy with developers you recruit without asking these “geeky” questions, then good for you. The point is that you try to find the hire that is sufficient for the positions that you expect. But my needs may be different than yours and therefore those I hire may not need to expose the same characteristics as those you hire. In my place, we need people who can quickly learn new technologies, platforms, and languages to quickly switch among various projects and the strong foundation knowledge would help with the transition (e.g. a guy knows the internals of Java and the JVM are more likely to learn .NET and the CLR). Or for a more specific example, if you are looking for people who will work in a multi-threaded application, then questions like “what is the difference between Vector and ArrayList in Java” would reveal whether the person has some understanding about concurrency programming in Java - while that same question may be irrelevant (or unnecessary “geeky”) in other applications in which people don’t care whether Vector or ArrayList is used.
Thanks for your reply. You say you can confirm there’s a “perfect reason for the combination of the internal and and protected” keywords but then you don’t, and after I read that question, all I found on the MSDN forums from Microsoft .NET experts is that you don’t want to combine them. So I’m really curious what this “perfect reason” is, when everything I’ve read is that you don’t want to combine them, and indeed, the VS Studio IDE team is going to make this an error in a future release?
I still think it’s an invalid statement that you would never want to use, and that if you read MSDN forums, the fact you can write such a statement is invalid and should throw up a warning, just like trying to declare a “private static const”. The “protected” keywork indicates one scope, the “internal” another, and the twain shalt never meet.
I guess the reason this raises my ire is that in the last 4-5 years of my career I’ve been bouncing between being very high level and hiring people to being a low-level “Code Jockey” being hired, and your question is similar to many I’ve run into in interviewing this past six months for a full-time job, having decided that freelancing will be the death of me.
That conclusion is that I get asked questions like the one you brought up that is not really a valid question other than “VS should complain about it but doesn’t” - I’ve got 25+ years of experience in this field, and I’m tired of being interviewed by people who don’t know what they’re talking about but somehow have managed to squirrel their way into a very high-level position, when I’m a firm believer that a Team Lead or Manager should be able to, or at least have done, 80% of what the team he/she is hiring has and will. But instead I’ve run into questions like yours that make me think “this person will be my manager, or my manager’s manager, but will never understand what I’m doing.”
Your question, I still insist, is invalid, and the answer is “Don’t do that.” To try to declare the scope of a const or variable to be both only within that class and subclasses *and* also be available to any class in the C# project, which is what “internal” means, at least in .NET 3.5 - well, obvious “internal” covers what “protected” means and therefore makes it irrelevant.
It’s like the VP of Eng at a company I’ll name, since the employees and managers seemed like they all knew what they were doing despite this obviously clueless VP somehow getting a job there - at one point in the interview, he said they were switching their customer-facing Web pages from PHP to Java, so of course, I asked if my JSP experience would help with that. And what I got was a look of confusion, and then I was asked “what JSP had to do with it?” and that they were going to generate Web pages “directly from Java.”
The manager of that team of course rolled his eyes and said “Yeah, it means switching to JSP, he doesn’t get it, but we’ll get it done anyway.”
The difference is that unlike that manager, I cannot stand to be in the same room with someone like that who’s talking out their arse but making 2x my salary plus 10x stock options and who knows what kind of bonuses.
So, ultimately, I’m suggesting that instead of that question being a good one to ask, instead it shows what I believe is a lack of understanding on your own part of how .NET and C# works, and that it reveals yet another person who’s been hired and/or promoted beyond their skill level (known as “The Peter Priniciple”, a term coined in the 1960’s.)
Give me one valid instance of when you would ever use “internal” and “protected” together and I’ll happily retract all statements and change my mind. Of course, you might want to try to convince the VS IDE Team at Microsoft too while you’re at it. But for now, methinks The Emperor Has No Clothes.
That said, I will say that at least half of your list are *excellent* questions. It’s just the ones that aren’t that make me wonder if you really know enough about C# and .NET to be interviewing someone about it?
In conclusion, I just offer an opinion, mean no offense or insult, and Peace. Cheers mate! Wink
@Wink Jr:
Again, thanks for your comment. I like to receive different views and feedbacks and definitely don’t find your opinion offensive at all.
Regarding the use of
protected internal, the only reason that I can *confirm” is because I have written such code and observed the behavior many times (instead of simply trusting some arbitrary articles or forum Q&As). In simple words, members declared as protected internal is accessible by any type in the defining assembly OR any derived type regardless of assembly.And even if you don’t want to type the code in and assert the behavior, a quick look at the language specification reveals the followings:
I don’t trust all Q&As in the forum though, just because some “expert” says “You should not” does not mean you should not - who knows whether the folk really understands what s/he’s talking about. I’d prefer do my own homework (fire up VS.NET, type the code in, observe the behavior, ildasm it, or refer to the language spec or books by guys like Jeffery Richter) rather than simply listen to some arbitrary people in forums.
Finally, I’ve just fired up my VS.NET & ildasm to verify the claim you have for changes in .NET 3.5. And I can confirm that there’s no such change and the
protected internalmembers in C# 3.0 behave exactly as in C# 1.0/2.0. (BTW, as the IL generated is the same in these versions of C#, this accessibility behavior in other CLR languages like VB.NET should also be the same. But I have not written the code to check that myself, thus I will not confirm.)Peace!
Let me ask you .NET question.
You’re programming in C# - 1.0, 2.0 or 3.0. You could be running on .NET 1.0, 1.1, 2.0, 3.0, or 3.5 at least. How does your application tell what version of .NET its running on, and how do you write code that will run on at least .NET 1.1, .NET 2.0, 3.0 and 3.4?
Clue: the answer would take you an hour to explain. I’m looking forward to hearing your answer to my interview question as to why I would want to work for you / why you should keep your job, plus the answer to why you would ever combine
internalandprotectedwhen the former is a superset of the latter, which you deftly avoided answering in your reply.That, or you can admit you don’t know. But I’m sure that would never happen.
And that’s actually a point worth making about interviewing and finding good employees - I’ve hired maybe two dozen people in my career, and every one of them that at some point was honest enough to say “I won’t B.S. you - I don’t know” - every one of them is someone I’m still in touch with and would work for, with, or hire again in a second. Because “I don’t know” is often the Right Answer.™ At least in the Real World.
Cheers, Wink
Thank you SO VERY MUCH for not being offended by my questions! You rock! I’ve linked to this article on my blog twice but I think now I’ll have to write an entire entry - I’m much like you, I love to hear different ideas and opinions. You’re a smart guy, but I’m not, and I’ll be the first to admit I’m more often wrong than I am right. Thanks for being so COOL.
P.S. My last post got repeated twice, can you delete the second one?
P.P.S. Great answer! With your permission (here or via email) I would like to throw it at some of the people at Microsoft on the Visual Studio team!
PPP.S. Again, thanks for not thinking I’m just being a jerk. So many people on the Internet these days (I go back to USENET in 1984) assume everything is meant to be confrontational, and it’s nice to meet a fellow spirit who understands the old Greek questions/answer/debate method of learning (I know there’s a Greek & English term for it, but it’s almost midnight on Sunday and my brain - she be broke!) Anyway, cheers for being chill!
Well, I called a pal at Microsoft and ran your concepts of “internal”, “protected” and “internal protected” by him - yes, like me, he’s up at 11PM on a Sunday normally.
You wrote about the latter: Protected internal, which is selected by including both a protected and an internal modifier in the member declaration. The intuitive meaning of protected internal is “access limited to this program or types derived from the containing class”.
His take was this: exactly. Access is limit to this “Solution” (he said “program was not the correct term”) - “or” - he said “should be and, really” the types derived - but again, what he said agreed with my answer - all access limited to solution includes “types or subclasses derived from the containing class”. So the “internal” is a superset of “protected” and in VS 2008 supercedes “protected” - so it’s moot. Or as he put it, “a ***ing waste of time.” I’ll save you the rest of his ranting.
I don’t work at Microsoft because of that kind of culture.
I tried to debate him to get any subtle difference in “any way” but he said “if you declared ‘internal’, VS will ignore ‘protected’ so it’s a waste of code. It’s like a comment except it means nothing.”
Still, the more I think about it, the more if falls into my idea of when I’d rather hear someone say “I don’t know.” I stand firmly by my belief that out of all the people I’ve ever been involved in hiring, the ones that said “I don’t know” at some point have turned out to be, without exception, excellent hires.
This has been an excellent discussion. Thank you so very much!
@Wink Jr.:
Well, I’m afraid your MS friend is not correct about this. It’s not VS.NET *solution* that the language specification refers to by the term “program”, it’s the *assembly* (thus expose behavior of
internalkeyword). Note that a VS.NET solution may or may not map to one assembly. (I’d typically prefer to avoid VS.NET concepts when discussing CLR stuffs to avoid confusion since the CLR can live well without VS.NET.) On the other hand, please also note that types or subclasses derived from the containing class can come from other assemblies (thus expose behavior ofprotectedkeyword).In short, as mentioned in the earlier response: members declared as protected internal is accessible by any type in the defining assembly OR any derived type regardless of assembly.
The easiest way to verify it:
- Create a VS.NET solution.
- Create 2 projects (by default, VS.NET will build each project to a separate assembly).
- Create a type called
Basein Project 1 with aprotected internalfield calledname(of any type).- Create a type
Otherin Project 1. Inside a method ofOther, instantiate aBaseand you will seeOthercan access to thenamefield of theBase‘ instance.- Create a type
Subin Project 2 and inheritBase. You will seeSubcan access to thenamefield ofBase, even thoughSubis in a different assembly.- Now, if you remove the modifier
protectedfromnamefield ofBase. The code ofSubwon’t compile any longer because of visibility insufficiency.great site with good info
Here is a good collection of .Net 3.0 Interview Questions
http://www.oopsconcepts.com/Net+3+Interview+Questions+and+Answers
Here is a good collection of .Net 3.0 Interview Questions and Answers
http://www.oopsconcepts.com/Net+3+Interview+Questions+and+Answers
Here’s an interview technique no one that I know of has ever tried, but which I think would be very good at determining how much an asp.net developer knows. Simply give the person a listing of an .aspx file from a web site and ask him or her to go line by line and explain everything they can. They don’t have to figure out what it’s doing, just show that they’re familiar with the various server controls, html statements, etc. If you wanted to test someone’s C# knowledge, hand them some code and ask them to tell you whatever they can. Again, they don’t need to figure out what it’s doing - the idea is by them describing whatever they can about the code, you get a good idea of what they know.