将新项目添加到列表对象C# [英] Add new item to List object C#

查看:54
本文介绍了将新项目添加到列表对象C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将新对象添加到列表中:我的代码:

I want to add new object to a list: My code:

List<geo_tag> abc = new List<geo_tag>();
abc.Add(new geo_tag() { latitude = 111, longitude = 122, unit = "SSS" });   

运行时出现错误:

编译器错误消息:CS1026:)应该在第2行出现.

Compiler Error Message: CS1026: ) expected at line 2.

使用.Net 2.0

Using .Net 2.0

推荐答案

您正在使用的对象初始化程序语法是C#3.0附带的.对于2.0,您必须使用

The object initializer syntax you are using came with C# 3.0. For 2.0 you have to use

List<geo_tag> abc = new List<geo_tag>();
geo_tag tag = new geo_tag();
tag.latitude = 111;
tag.longitude = 122;
tag.unit = "SSS";
abc.Add(tag); 

这篇关于将新项目添加到列表对象C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆