Click here to Skip to main content
15,899,825 members
Home / Discussions / C#
   

C#

 
GeneralRe: access to localdb from different pcs in a lan network Pin
OriginalGriff28-Aug-15 2:02
mveOriginalGriff28-Aug-15 2:02 
GeneralRe: access to localdb from different pcs in a lan network Pin
Member 1189642728-Aug-15 2:19
Member 1189642728-Aug-15 2:19 
GeneralRe: access to localdb from different pcs in a lan network Pin
OriginalGriff28-Aug-15 2:29
mveOriginalGriff28-Aug-15 2:29 
AnswerRe: access to localdb from different pcs in a lan network Pin
Gilbert Consellado27-Aug-15 16:11
professionalGilbert Consellado27-Aug-15 16:11 
GeneralRe: access to localdb from different pcs in a lan network Pin
Member 1189642727-Aug-15 19:05
Member 1189642727-Aug-15 19:05 
GeneralRe: access to localdb from different pcs in a lan network Pin
Gilbert Consellado28-Aug-15 5:46
professionalGilbert Consellado28-Aug-15 5:46 
AnswerRe: access to localdb from different pcs in a lan network Pin
Eddy Vluggen28-Aug-15 2:04
professionalEddy Vluggen28-Aug-15 2:04 
QuestionLooking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer26-Aug-15 8:06
professionalclientSurfer26-Aug-15 8:06 
Hi Everyone,

No this is not a homework question; not even sure if this question belongs in here or if anyone has the time to have a look over these, but here's the situation: I was recently provided with 5 technical questions over eMail from a recruiter for a very large video game company for me to answer and return to the recruiter, who was supposed to call me and go over my answers but hasn't done so yet. So of course I'm getting paranoid as I sit waiting for some sort of response regarding my answers and so I thought, heck, why not post the questions and my answers somewhere here on CodeProject in the hopes that at least one of the mad experts in this community might have a few minutes just to read through my answers and critique them (I mean really let me have it if need be). I've been developing for a long time but you know how the interview process goes, every day that you have to wait for a response is agonizing and can cause you to start doubting yourself.

So at the risk of this being in the wrong area of CP, here are the questions that I was given and the answers that I came up with (with a surprising minimum of Googling and any kind of word-for-word parroting of things that I've read and assimilated over the years):

1. In C#, what is an interface?

An Interface in C# is a way to achieve polymorphism within modules so that similar classes that need to readily utilize methods implemented in other common classes can do so without having to incur the overhead of instantiating the other classes within themselves. Interfaces are type declarations that can contain collections of declarations of methods, events, properties and indexers without their implementations; the actual implementations are left to any class that chooses to implement that interface. And since interfaces are technically contracts guaranteeing that any class that implements said interface MUST contain actual implementations of each and every method, event, property and indexer declared in the interface (with identical signatures). Interfaces are a valuable way of making code extensible and introducing abstraction into multiple classes that have a need for executing common tasks with slightly different implementations. The Interface concept can be counter-productive if it declares any methods that are specific and relevant to only one class, as the purpose of the Interface is to eliminate redundant method implementations between more than one class that must use said method. This is why interfaces best contain methods, properties, etc., that perform a related set of functionalities that need to be available to as many classes as possible to provide design abstraction, but only if the abstraction is actually necessary and does not lead to undue increased complexity and difficulty maintaining reusable code.

2. What does the “using” statement do in C#? (note: we are referring to its usage in code blocks, not when used to import namespaces).

The using statement in C# (not to be confused with the using directive to import namespaces) is used to provide a shorthand way to simplify the code necessary to properly encapsulate the acquisition, usage and proper disposal of a resource used within an object. At the IL level, the using statement is actually translated by the CLR (or expanded so to speak) into an instantiation of the required resource into an object, followed by an implementation of a try{}...finally{} block in which the execution of a method on that object's resource is attempted in the try{} portion and the disposal of the object containing the resource is performed in the finally{} portion by calling the object's Dispose method which is accessed via the IDisposable interface, provided that the resource is not null which would result in a run-time error since in that case there would be no object on which to call a Dispose method.

3. What does it mean to you when I say strings are immutable?

In .NET all variables of type string are immutable, or unchangeable, meaning that once the variable has been assigned a value, you can never actually change its value. This is due to the fact that the string datatype in .NET is actually a reference type which is a pointer to a memory location containing the contents of the string and then allocated in the heap until the variable comes into scope and the pointer therein is pushed onto the stack. So when you try to assign a new value to a string variable that already has been assigned an initial value, a new memory location is actually allocated in the background and the pointer to that new memory location is merely copied to our string variable which, in the foreground, appears as if the actual value of the variable has been changed. This characteristic can cause misuse and confusion by developers who are used to other languages in which operators such as the concatenation (+) operator can be used on a string variable directly. Hence in .NET the preferred (if not downright mandatory) way to essentially achieve string mutability is through the use of the System.Text.StringBuilder, which is actually not a string but a class containing a collection of character data along with methods that can perform mutable operations (concatenations, manipulations, etc.) on said character data collection and that can ultimately be finally converted into an actual immutable string type by using Object.ToString() on the instantiated StringBuilder object.

4. In C# in the Task Parallel Library, what is a Task?

In the TPL a Task is a class that when instantiated becomes a lightweight object that incurs minimal overhead by, instead of spawning individual threads that have their own overhead and initialization latencies and requirements for exception handling and propagation, utilizes the CLR's ThreadPool class (which can also be used manually using the ThreadPool.QueueUserWorkItem method directly but since .NET 4.0 the entire ThreadPool class has been overhauled to be specifically optimized for maximum efficiency when used intrinsically by Tasks. Tasks were designed for leveraging multi-core processors to efficiently manage parallelizable units of work (in fact the powerful Parallel class is directly based on the concepts of task parallelism) by containing powerful features such as tuning of a task's scheduling, starting of tasks within tasks to create parent/child task relationships that allow for such things as implementing cooperative cancellation between tasks, waiting on a whole set of tasks to complete without the need for signaling constructs such as delegates or heaven forbid, old-school CPU-intensive techniques like polling loops for thread results LOL), attaching of "continuing tasks" to be automatically started upon completion of one or more antecedent tasks, and automatic propagation of task exceptions to parents, continuations and actual task consumers.

5. What is the difference between a Mutex and a Semaphore and give an example of where you would use each.

The major difference between a Mutex and Semaphore is that a Mutex ensures that one and only one thread can access a resource (or section of code) at a time whereas a Semaphore establishes a maximum number of concurrent threads that are allowed to simultaneously access a resource by basically maintaining a queue of threads to enforce concurrency in an ordered fashion. Another important difference is that Mutexes, like simple locks, can only be released by the same thread that obtained the lock in the first place, whereas Semaphores have no owner; i.e., are "thread-agnostic", such that any thread can call Release on a semaphore regardless of which other thread may have obtained it initially. And while Mutexes and Semaphores are similar in the respect that both are thread-locking constructs that can be cross-process friendly, allowing them to work computer-wide as well as application-wide, Semaphores must be named in order to span processes in the same way that Mutexes can intrinsically. An example of a classic use for a Mutex would be to leverage its computer-wide synchronization and locking mechanisms to prevent more than one instance of an application from being allowed to be executed at the same time. An example of an appropriate use of Semaphores would be to implement a simple lightweight message-queueing data structure that could be easily used in situations in which multiple threads within a single application need access to the same message queue but where a full-blown MSMQ implementation would be overkill in terms of not only resource overhead but also would perform way too slowly to be practically used in real-time distributed applications such as those that may require, say, socket-level reading of UDP messages that each might require processing in a separate thread ASAP.

So that was my best attempt late at night on these; I welcome any critiques/corrections/comments/complaints/insults, etc., from anyone who has the time to wade through my rather verbose answers... Please be as brutal as necessary - I aint' skeered, just want to always keep learning and you guys are the best!

Thanks in advance CPians! Cheers
 "... having only that moment finished a vigorous game of Wiff-Waff and eaten a tartiflet." - Henry Minute
 "Let's face it, after Monday and Tuesday, even the calendar says WTF!" - gavindon  
Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning. - gavindon

AnswerRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Dave Kreskowiak26-Aug-15 8:39
mveDave Kreskowiak26-Aug-15 8:39 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer26-Aug-15 9:16
professionalclientSurfer26-Aug-15 9:16 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Dave Kreskowiak26-Aug-15 10:27
mveDave Kreskowiak26-Aug-15 10:27 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer26-Aug-15 12:37
professionalclientSurfer26-Aug-15 12:37 
JokeRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer26-Aug-15 9:21
professionalclientSurfer26-Aug-15 9:21 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Dave Kreskowiak26-Aug-15 10:28
mveDave Kreskowiak26-Aug-15 10:28 
AnswerRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
F-ES Sitecore27-Aug-15 0:50
professionalF-ES Sitecore27-Aug-15 0:50 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer27-Aug-15 6:21
professionalclientSurfer27-Aug-15 6:21 
AnswerRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Richard MacCutchan27-Aug-15 1:41
mveRichard MacCutchan27-Aug-15 1:41 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer27-Aug-15 6:22
professionalclientSurfer27-Aug-15 6:22 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Eddy Vluggen28-Aug-15 9:20
professionalEddy Vluggen28-Aug-15 9:20 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer29-Aug-15 16:35
professionalclientSurfer29-Aug-15 16:35 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
Eddy Vluggen30-Aug-15 2:16
professionalEddy Vluggen30-Aug-15 2:16 
JokeRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer30-Aug-15 6:31
professionalclientSurfer30-Aug-15 6:31 
AnswerRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
BillWoodruff28-Aug-15 18:21
professionalBillWoodruff28-Aug-15 18:21 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
clientSurfer29-Aug-15 16:48
professionalclientSurfer29-Aug-15 16:48 
GeneralRe: Looking for Critique of My Answers to Recent C# Multithreading Interview Questions... Pin
BillWoodruff29-Aug-15 23:29
professionalBillWoodruff29-Aug-15 23:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.