I Need some clarifications about:
is/as operators
out/ref parameters modifiers
typeof
Feel free to reply only to what you know or what you want to reply and not the whole question if you don't feel like.
Yes, I'm asking clarifications about these. Correct me or add to what I've already said.
Well, I've googled a bit and landed up on various sites, MSDN and so on. I still need someone to write up some code to help me clarify the whole concept and finally let me use it in its full power, hoping that this will be helpful for others too.
Basically this is what I've understood:
A) is
In pseudocode
If object is this type return true, else false
Easy. Or I am doing something wrong here?
Are there any limitations or problems with some types?
This code runs fine, so I guess I got this good.
object s = new SqlBoolean();
if (s is SqlBoolean)
{
Console.WriteLine("Is SqlBoolean");
Console.ReadLine();
}
2) as
Well, I still have to fully understand this.
MSDN Reports:
The as operator is used to perform conversions between compatible types. The as operator is like a cast except that it yields null on conversion failure instead of raising an exception.
Well I tried a loop on an Object array and it basically converts only primitives and with lot of problems (every casting more than a little difficult throws null or something)
I.e
using System;
using System.Data.SqlTypes;
class StackOverflow
{
public static void Main()
{
string country = "jii";
string vatNum= "dde";
object[] MyObjects = {country,
vatNum
};
for (int i = 0; i<=MyObjects.Length-1;i++)
{
MyObjects[i] = MyObjects[i] as SqlString?;
Console.WriteLine(MyObjects[i].ToString() + " " + MyObjects[i].GetType());
Console.ReadLine();
}
}
}
In a program I am making I need those values to be converted into SqlStrings, SqlBooleans and so on.
Pay attention at the fact that I'm forcing SqlString to Nullable with the line "SqlString?" with the question mark at the end (? forces to nullable, who knew it! :D).
So basically it's useful for doing some low level conversion without using exceptions? Or what?
3) ref and out
Well basically ref changes the value of a predeclared variable from within a function:
IE
using System;
class StackOverflow
{
public static void Main()
{
int x = 10;
Write(func(ref x).ToString());
Write(Convert.ToString(x));
}
public static int func(ref int y)
{
y += 10;
return y;
}
public static void Write(string z)
{
Console.WriteLine(z);
Console.ReadLine();
}
}
Executing this will be ok and output will be 20 20 removing both ref will output 20 10
(ref changes the externally declared x directly without making an instance in the method)
What about out? What's the use????
I can't get it. Reading somewhere it seems it's like a parameter that does the same of ref, but it lets you somehow have some output parameter.
Putting out instead of ref in the previously shown code will only produce compilation errors by IntelliSense and I don't even know how to get those out parameters to show.
In a project I'm doing I'm using the following code:
public static Array check(string country, string vatNum)
{
bool valid;
string name;
string address;
checkVatService vatchecker = new checkVatService();
vatchecker.checkVat(ref country, ref vatNum, out valid, out name, out address);
return new object[] {country,
vatNum,
valid,
name,
address
};
}
Now, checkVat is some code from the european online checking webservice.
I see it has out parameters and they are generated by the wsdl webservice consuming library from the webservice website (link to library, if needed)
How do I use the out parameters? Are them something to have some output without using return? How I use them? Please be very detailed
4) typeof
This returns the type from System.Type of the Type you pass to it.
First I thought I could get the type of any object I passed to it like
int x;
SqlBoolean xy = new SqlBoolean();
typeof(x) // integer
typeof(xy) // SqlBoolean
but it's not that.
It just returns the type from System.Type.
Like
typeof(int).ToString() // System.Int32 or something like that typeof(xy).ToString() // System.Data.SqlTypes.SqlSqlBoolean
and I don't really know what to do with that.
For getting the type of an object you gotta do something like .GetType from an object or so.
So what's the pratical use of typeof and what am I missing about it?
Thanks anyone