Interview Tips Interview Tips, Interview Questions and Answers

11May/110

What are the fundamental differences between value types and reference types?

C# divides types into two categories – value types and reference types. Most of the intrinsic types (e.g. int, char) are value types. Structs are also value types. Reference types include classes, arrays and strings. The basic idea is straightforward – an instance of a value type represents the actual data, whereas an instance of a reference type represents a pointer or reference to the data.The most confusing aspect of this for C++ developers is that C# has predetermined which types are represented as values, and which are represented as references. A C++ developer expects to take responsibility for this decision

25Dec/100

Custom Exceptions in .NET

The .NET Framework provides a rich set of system-defined exception types that can be thrown and caught by the C# developer. However, the list of available exceptions does not cover every eventuality and often it is more appropriate to define custom exceptions for specific error scenarios. This can be achieved by deriving a new exception class from any of the existing types, usually the ApplicationException class.

In the final section of this article we will define a new exception class and demonstrate how it may be thrown and handled. This will be a simple exception with no custom methods or properties. These can be added to the derived class using standard object-oriented programming techniques. However, this is beyond the scope of the C# Fundamentals tutorial and will instead be described in a future object-oriented programming tutorial.

12Mar/100

simple C# interview question that most developer fails

How do you define a property read only for the outside world and writable for the same assembly classes?

For example I have a class named User where everyone outside the assembly can read the string ‘Name’ property but cannot set it. However the classes inside the assembly is able to set the property.  I am further detailing the exlanation.

User myUser = SomeClass.GetUser();

// OK for all classes since all can read it
string name = myUser.Name;   

// This line does not compile if this code is
// written in a class that is not in the same
// assembly as the type User. But it compiles
// if the code is written in the same assembly
// that contains the type user.
myUser.Name = "C# Developer";

Answer:

Now all I want is the c# code declaration for the property name that matches my requirement of being read only for the outside world. Write it in the comments section. I will answer the question 24 hours from now.