C# "as" operator and advanced conversion use in loops/functions and out/ref parameters modifiers and typeof() -


this question has answer here:

i need clarifications about:

is/as operators

out/ref parameters modifiers

typeof

feel free reply know or want reply , not whole question if don't feel like.

yes, i'm asking clarifications these. correct me or add i've said.

well, i've googled bit , landed on various sites, msdn , on. still need write code me clarify whole concept , let me use in full power, hoping helpful others too.

basically i've understood:

a) is

in pseudocode

if object type return true, else false

easy. or doing wrong here?

are there limitations or problems types?

this code runs fine, guess got good.

object s = new sqlboolean();         if (s sqlboolean)         {             console.writeline("is sqlboolean");             console.readline();         } 

2) as

well, still have understand this.

msdn reports:

the operator used perform conversions between compatible types. operator cast except yields null on conversion failure instead of raising exception.

well tried loop on object array , converts primitives , lot of problems (every casting more 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                              };          (int = 0; i<=myobjects.length-1;i++)         {             myobjects[i] = myobjects[i] sqlstring?;             console.writeline(myobjects[i].tostring() + " " + myobjects[i].gettype());             console.readline();         }     } }  

in program making need values converted sqlstrings, sqlbooleans , on.

pay attention @ fact i'm forcing sqlstring nullable line "sqlstring?" question mark @ end (? forces nullable, knew it! :d).

so it's useful doing low level conversion without using exceptions? or what?

3) ref , out

well ref changes value of predeclared variable within 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 ok , output 20 20 removing both ref output 20 10

(ref changes externally declared x directly without making instance in method)

what out? what's use????

i can't it. reading somewhere seems it's parameter same of ref, lets somehow have output parameter.

putting out instead of ref in shown code produce compilation errors intellisense , don't know how out parameters show.

in project i'm doing i'm using 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 code european online checking webservice.

i see has out parameters , generated wsdl webservice consuming library webservice website (link library, if needed)

how use out parameters? them have output without using return? how use them? please detailed

4) typeof

this returns type system.type of type pass it.

first thought type of object passed like

int x; sqlboolean xy = new sqlboolean(); typeof(x) // integer typeof(xy) // sqlboolean 

but it's not that.

it returns type system.type.

like

typeof(int).tostring() // system.int32 or typeof(xy).tostring() // system.data.sqltypes.sqlsqlboolean

and don't know that.

for getting type of object gotta .gettype object or so.

so what's pratical use of typeof , missing it?

thanks anyone

large question.. small answer:

  1. correct.
  2. it allows casts fail without exception. therefore, can check if result null. if is, cast failed.
  3. ref passes objects reference (note: still pass-by-value semantics though!). out means variable must assigned before function exits.
  4. i'm not sure understand issue typeof. might best disregard use if don't understand why might useful (as anything, really).

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -