tags:

views:

353

answers:

8

We know that string is a reference type , so we have

string s="God is great!";

but on the same note if i declare class say Employee which is a reference type so why below piece of code does not work ?

Employee e = "Saurabh";

2- How do we actually determine if a type is a reference type or value type?

A: 

The "code below" doesnt work because you are trying to set e to a string, when e is of type Employee. Type safety prevents this.

Chris
*All variables in C# are reference types.* — There are some value types. http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
KennyTM
'Scuse me? What happened to value types? Like, i dunno, int and char?
Seva Alekseyev
Incorrect: If a string would be a reference then passing a string to a method would allow the method to change the string which is not the case.
dbemerlin
This isn't exactly true. C# has two different value types: Structs and Enumerations (http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx). Also, structs are categorized as integral (int, long, char, etc), floating point, and decimal; as well as bool and user defined structs.
Chris Lively
@dbemerlin: No, that's not true. string is a reference type which is perfectly ordinary in many ways; it happens to be immutable - but you can create your own immutable reference types too.
Jon Skeet
I was trying to keep things simple as obviously the OP doesn't understand type safety.
Chris
@dbemerlin - Strings are still reference types, but they are also immutable, hence why passing a string to a method does not allow the method to change the string.
LeguRi
@Chris: I don't think that claiming "every variable in C# is a reference" is going to help the OP :(
Jon Skeet
+2  A: 

Reference types are not assignable unless they are of the exact same type (this is known as type safety). The first example works because you are assigning a string literal to a variable of the type System.String. The second example does not work because you are assigning a string literal to a variable of the type Employee. The types must match or be assignable from right to left for value assignment to work.

Andrew Hare
Same type or subtype. You can assign an DeveloperEmployee to an Employee (if DeveloperEmployee inherits from Employee).
dbemerlin
Right - that is what I meant by assignable.
Andrew Hare
why are you stating "reference types are not assignable unless of the same type"? Type safety rules are the same for value types so why confuse the confused even more?
Marek
+2  A: 
Employee e = "Saurabh";

will not work simply because they are of different types.

Otávio Décio
Add an implicit operator to Employee and this will work.Assume that Employee has a constructor that takes a string parameter:public static implicit operator Employee(string name){ return new Employee(name);}
Russ C
+14  A: 

That code would work if you had an implicit conversion from a string to an Employee. Basically a string literal is of type string - i.e. its value is a string reference (and an interned one at that). You can only assign a value of one type to a variable of another type if there's a conversion between the two types - either user-defined or built in. In this case, there's no conversion from string to Employee, hence the error.

Contrary to some other answers, the types don't have to be the same - for example, this is fine:

object x = "string literal";

That's fine because there's an implicit reference conversion from string to object. Likewise you can write:

XNamespace ns = "some namespace";

because there's an implicit conversion from string to XNamespace.

To answer your second question: to see if a type in .NET is a value type or a reference type... struct and enum types are value types; everything else (class, delegate, interface, array) is a reference type. That's excluding pointer types, which are a bit different :)

Jon Skeet
+1 Excellent example of implicit conversion with `XNamespace`.
Andrew Hare
Good explanation .. and i got rejected due to this question , it was really embrassing for me to not answering this basic question , i screwed up !!!
saurabh
I find it hard to believe that microsoft is asking such simple questions (although they are tricky). But this is nothing compared to the google interview.
JonH
that's true but sometime basics are important and many people lack in basics like i did :(
saurabh
@Marek: See saurabh's first comment.
Jon Skeet
@Jon sorry, I somehow oversaw that comment :)
Marek
Jon - Don't your two examples work for different reasons, the first because string derives from Object, and the second because namespace defines a conversion from string...or is this all the same under the hood?
kekekela
@saurabh - I don't let it bring you down, MS is not the only place of work. Take it as a learning experience and a way to move on. Maybe it was for the better :).
JonH
@JonH : Yup , i moved on now and trying to refresh my concepts once more. :) thanks.
saurabh
All this time, I've never really questioned this. I've learned something today, thanks!
Darryl
+4  A: 

Because they're not the same type, if you define a TypeConverter then that would work.

http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx

kekekela
haha, thanks Steven, glad to see you're still obsessed, you pathetic freak.
kekekela
+2  A: 
object x;

x = new Employee();

x = "Hello World!";
Robin Day
A: 

Here one more basic question

Class A {

public virtual void Method1(){}

public void Method2() {

Method1(); }

}

class B:A { public override void Method1() { } }

Class main

{

A obk = new B(); obk.Method2(); }

now tell me which function gets called ? sorry for the typos.

saurabh
Stop answering your question with more questions. You should post this as a new question.
Bill
thanks Bill and i will remember this next time, sorry for this mitake
saurabh
A: 

To determine whether a type is a reference type or not...

    string s = "foo";
    bool sIsRef = !s.GetType().IsValueType;  // true

    int i = 100;
    bool iIsRef = !i.GetType().IsValueType;  // false
Darryl