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:

  1. methods in c# conventionally named in titlecase, e.g. getpointsbytime , not getpointsbytime.
  2. 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 inheriting list<pointdata> or, preferably, using ilist<pointdata> member in new pointcloud class). or use using system.collections.generic , use list<pointdata> throughout code, people do.
  3. 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

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

android - MPAndroidChart - How to add Annotations or images to the chart -