c# - What's the difference between Process[] and Process()[] -
i doing research pattern scanner came problem.
the pattern scanner saw needed handler of process way i'm doing
process[] p = process.getprocessesbyname("pname");
doesn't have p.handle
, went msdn there says have. why mine doesn't?
what difference between 2 lines of code?
process p = process.getprocessesbyname("pname")[0]; process[] p = process.getprocessesbyname("pname");
this gets first process has name of "pname":
process p = process.getprocessesbyname("pname")[0];
note cause "index out of bounds" exception if there aren't any.
this gets list (or array, actually) of processes have name of "pname":
process[] p = process.getprocessesbyname("pname");
note won't cause exception if there aren't any; it'll return empty array.
with latter, can index former, if want first match:
process[] p = process.getprocessesbyname("pname"); if (p.length > 0) { process process = p[0]; //get first 1 } else { log("no such process!"); }
or (the way it) use linq:
process p = process.getprocessesbyname("pname").firstordefault(); if (p != null) { //do process }
Comments
Post a Comment