I am a C#-Newbie.
我是C# - 新手。
I have a function that is supposed to return all values from a List, that have the matching time-stamp:
我有一个函数应该返回List中的所有值,它们具有匹配的时间戳:
static public PointCloud getPointsByTime (float time)
{
PointCloud returnList = new List<PointData> ();
for (int i = 0; i < _pointCloud.Count; i++) {
if (_pointCloud [i].time == time) {
returnList.Add (_pointCloud [i]);
}
}
return returnList;
}
Where
public class PointData
{
public float time;
// and some other members
}
and
// let's call a list of PointData-objects a PointCloud
using PointCloud = System.Collections.Generic.List<PointData>;
Does my function do what I want it to do? Or do I have to create a new PointData
-object? Am I able to use my returned PointCloud
or will it be out of scope and deleted?
我的功能是否符合我的要求?或者我是否必须创建一个新的PointData对象?我可以使用我返回的PointCloud还是超出范围并删除?
This may be not the best example to explain so feel free to link me to something better. I think you get what my basic quastions are.
这可能不是解释的最好例子,所以请随意将我链接到更好的东西。我想你得到了我的基本规则。
2 个解决方案
#1
Your code is correct. You can use your function like so:
你的代码是正确的。您可以像这样使用您的函数:
var someTime = 0.0f;
var pointsAtTime = getPointsByTime(someTime);
DoSomethingWith(pointsAtTime);
The return value from the function remains in scope if you assign it to some local variable (e.g. pointsAtTime
here).
如果将其分配给某个局部变量(例如,此处为pointsAtTime),则函数的返回值仍在范围内。
EDIT: As Peter Schneider correctly notes in the comments, you need to be aware that this function creates a new list with references to the matching points, and does not create new points. This might or might not be what you want.
编辑:正如Peter Schneider在评论中正确指出的那样,您需要知道此函数会创建一个新列表,其中包含对匹配点的引用,并且不会创建新点。这可能是也可能不是你想要的。
However, if you're new to C#, here are some things you might want to keep in mind:
但是,如果您是C#的新手,请注意以下几点:
- Methods in C# are conventionally named in TitleCase, e.g.
GetPointsByTime
and notgetPointsByTime
. - Assigning names to generic types like
using PointCloud = List<PointData>
, while technically allowed, is not very idiomatic and might confuse other readers of your code. If you think a list of PointData is special enough to have its own type, create a type for it (either by inheriting fromList<PointData>
or, preferably, using aIList<PointData>
as a member in a newPointCloud
class). Or just useusing System.Collections.Generic
and useList<PointData>
throughout your code, which is what most people would do. - Comparing floating-point numbers for equality is sometimes discouraged as this might fail in some cases due to representation errors; if the time is truly a continuous value, you might want to look for points in a specific time period (e.g. points who fall within some range around your desired time). You don't really have to worry about this for now, though.
C#中的方法通常在TitleCase中命名,例如, GetPointsByTime而不是getPointsByTime。
将名称分配给泛型类型,例如使用PointCloud = List
有时不鼓励比较浮点数是否相等,因为在某些情况下,由于表示错误,这可能会失败;如果时间确实是连续值,您可能希望在特定时间段内查找点数(例如,在您所需时间内的某个范围内的点数)。不过,你现在不必担心这个问题。
#2
As @Patrick suggested inheriting for List seems more reasonable, but I would go further and I would just use a List so you don't create an unnecessary class if it is not going to add anything extra.
正如@Patrick建议继承List似乎更合理,但我会更进一步,我只会使用一个List,所以你不创建一个不必要的类,如果它不会添加额外的东西。
Also I suggest you to have a look to LINQ which makes the code more readable and is a very powerful feature that you would like to master as soon as possible. :)
另外,我建议你看看LINQ,它使代码更具可读性,是一个非常强大的功能,你想尽快掌握。 :)
Your method could look then like this:
你的方法看起来像这样:
_pointCloud.Where(p => p.time == time).ToList();
Also try to get familiar with properties:
还尝试熟悉属性:
public class PointData
{
public float Time { get; set; }
}
And you may want to follow the more standard C# coding style (although this is completely personal) of using PascalCase for public members instead of camelCase.
并且您可能希望遵循更标准的C#编码风格(尽管这完全是个人的)将PascalCase用于公共成员而不是camelCase。
#1
Your code is correct. You can use your function like so:
你的代码是正确的。您可以像这样使用您的函数:
var someTime = 0.0f;
var pointsAtTime = getPointsByTime(someTime);
DoSomethingWith(pointsAtTime);
The return value from the function remains in scope if you assign it to some local variable (e.g. pointsAtTime
here).
如果将其分配给某个局部变量(例如,此处为pointsAtTime),则函数的返回值仍在范围内。
EDIT: As Peter Schneider correctly notes in the comments, you need to be aware that this function creates a new list with references to the matching points, and does not create new points. This might or might not be what you want.
编辑:正如Peter Schneider在评论中正确指出的那样,您需要知道此函数会创建一个新列表,其中包含对匹配点的引用,并且不会创建新点。这可能是也可能不是你想要的。
However, if you're new to C#, here are some things you might want to keep in mind:
但是,如果您是C#的新手,请注意以下几点:
- Methods in C# are conventionally named in TitleCase, e.g.
GetPointsByTime
and notgetPointsByTime
. - Assigning names to generic types like
using PointCloud = List<PointData>
, while technically allowed, is not very idiomatic and might confuse other readers of your code. If you think a list of PointData is special enough to have its own type, create a type for it (either by inheriting fromList<PointData>
or, preferably, using aIList<PointData>
as a member in a newPointCloud
class). Or just useusing System.Collections.Generic
and useList<PointData>
throughout your code, which is what most people would do. - Comparing floating-point numbers for equality is sometimes discouraged as this might fail in some cases due to representation errors; if the time is truly a continuous value, you might want to look for points in a specific time period (e.g. points who fall within some range around your desired time). You don't really have to worry about this for now, though.
C#中的方法通常在TitleCase中命名,例如, GetPointsByTime而不是getPointsByTime。
将名称分配给泛型类型,例如使用PointCloud = List
有时不鼓励比较浮点数是否相等,因为在某些情况下,由于表示错误,这可能会失败;如果时间确实是连续值,您可能希望在特定时间段内查找点数(例如,在您所需时间内的某个范围内的点数)。不过,你现在不必担心这个问题。
#2
As @Patrick suggested inheriting for List seems more reasonable, but I would go further and I would just use a List so you don't create an unnecessary class if it is not going to add anything extra.
正如@Patrick建议继承List似乎更合理,但我会更进一步,我只会使用一个List,所以你不创建一个不必要的类,如果它不会添加额外的东西。
Also I suggest you to have a look to LINQ which makes the code more readable and is a very powerful feature that you would like to master as soon as possible. :)
另外,我建议你看看LINQ,它使代码更具可读性,是一个非常强大的功能,你想尽快掌握。 :)
Your method could look then like this:
你的方法看起来像这样:
_pointCloud.Where(p => p.time == time).ToList();
Also try to get familiar with properties:
还尝试熟悉属性:
public class PointData
{
public float Time { get; set; }
}
And you may want to follow the more standard C# coding style (although this is completely personal) of using PascalCase for public members instead of camelCase.
并且您可能希望遵循更标准的C#编码风格(尽管这完全是个人的)将PascalCase用于公共成员而不是camelCase。