Is there somthing like 'out of scope' in C#? And some related questions -
i c#-newbie.
i have function supposed return values list, have matching time-stamp:
static public pointcloud getpointsbytime (float time) { pointcloud returnlist = new list<pointdata> (); (int = 0; < _pointcloud.count; i++) { if (_pointcloud [i].time == time) { returnlist.add (_pointcloud [i]); } } return returnlist; } where
public class pointdata { public float time; // , other members } and
// let's call list of pointdata-objects pointcloud using pointcloud = system.collections.generic.list<pointdata>; does function want do? or have create new pointdata-object? able use returned pointcloud or out of scope , deleted?
this may not best example explain feel free link me better. think basic quastions are.
your code correct. can use function so:
var sometime = 0.0f; var pointsattime = getpointsbytime(sometime); dosomethingwith(pointsattime); the return value function remains in scope if assign local variable (e.g. pointsattime here).
edit: peter schneider correctly notes in comments, need aware function creates new list references matching points, , not create new points. might or might not want.
however, if you're new c#, here things might want keep in mind:
- methods in c# conventionally named in titlecase, e.g.
getpointsbytime, notgetpointsbytime. - assigning names generic types
using pointcloud = list<pointdata>, while technically allowed, not idiomatic , might confuse other readers of code. if think list of pointdata special enough have own type, create type (either inheritinglist<pointdata>or, preferably, usingilist<pointdata>member in newpointcloudclass). or useusing system.collections.generic, uselist<pointdata>throughout code, people do. - comparing floating-point numbers equality discouraged might fail in cases due representation errors; if time continuous value, might want points in specific time period (e.g. points fall within range around desired time). don't have worry now, though.
Comments
Post a Comment