L
Lasse Vågsæther Karlsen
K said:Just for fun of it i tried to send in the value of null into a
constructor. It looks
like this.
public Donkey (Object ob, char ch) {}
The first call below works well but the second produces errors.
Donkey d1 = new Donkey (null, 'd');
Donkey d2 = new Donkey (null, null);
It's a good idea to also post which errors you're getting if you want
anyone to actually consider why you get it.
I'm assuming you get something along the lines of "Argument '2': cannot
convert from <'null>' to 'char'".
The reason is of course that Char is not a reference type, but a value
type, and hence null isn't a valid value for it.
I understand it's got to do with the fact
that char is a native _BUT_ there's a class in IntelliSence called Byte
and i
No, it has nothing to do with char being "native". char is the C#
keyword for the Char BCL type.
was convienced that "byte" was just a
syntactic sugar for "Byte", the same way "string" is for "String".
Well, yes, byte is for Byte what string is for String, same as what char
is for Char, etc.
1.
Can i/should i use "Byte" instead of "byte" (etc. for other types)?
Depends, it doesn't matter to the compiler or .NET. Personally I use the
..NET BCL types (Int32, Byte, String, Object, Double) instead of the C#
keywords (int, byte, string, object, double), but it's a matter of taste.
2. Aren't "byte", "int", "double" etc. really objects in this
OO-language, C#?
They're objects in the sense that they have methods, properties, etc. I
feel the hint of a different meaning of your question though, and I
think you're asking if those things are objects in the sense of
heap-allocated objects. In .NET and C#, they're not, they're value
types, which are allocated inline where you declare them (on the stack
in a method, in a class/struct when fields). You can box them to produce
an Object, like this:
Object o = 10;
which will produce a heap-allocated boxed Int32 object.
3. In Java, passing a native was passing by reference, while passing an
object was passing by value. Is it true
for C# as well?
I don't really know what a native is.
In .NET you can pass everything by reference or by value, however the
default convention is by value, and you need to explicitly state that
you want by reference by using either the ref or the out keyword.
Hence, the following three methods:
public void Test(String s)
public void Test(ref String s)
public void Test(out String s)
the first one passes by value, the two others by reference, and the
distinction between ref and out is basically wether it's the caller or
callee that is responsible for setting the value first.
Note that a common source of confusion in discussions about passing by
reference or value is what you can do with the objects being passed.
Take the following small class:
public class Class1 {
public Int32 Field1;
}
and the following two methods:
public void Test(Class1 c)
public void Test(ref Class1 c)
In both cases, the methods will be able to change the value of the
Field1 field of the object being passed in, passing by value or
reference have nothing to do with that.
But, the second method, passing by reference, can construct a new object
and replace the one being passed in with the newly constructed one. This
is the distiction.
In C++ you have const arguments as well, which prevents the callee from
modifying the contents of the object being passed as well, and this
concept does not exist in C#.
4. (off-topic)
Did anybody got the characters "R2", when regarding the second donkey?
Huh? (I understand the reference, just not where R2 would come into play
in this post)