Truong Do
Truong Do

Reputation: 69

Add new item to List object 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" });   

When run, it gets error:

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

Using .Net 2.0

Upvotes: 6

Views: 97376

Answers (7)

Khaled Eltabei
Khaled Eltabei

Reputation: 61

You can create a new object "class" and add items to it by tow ways:

private class Employee
{
    public int _id { get; set; }
    public string _Name { get; set; }
}

var EmpList = new List<Employee>();


EmpList.Add(new Employee { _id=1, _Name = "ahmed" }); 
// or
Employee Emp = new Employee();
Emp._id = 2;
Emp._Name = " Hany Samy";
EmpList.Add(Emp);

Upvotes: 0

Mahipal_Xode
Mahipal_Xode

Reputation: 31

class Program{
public static void Main(string[] args){
    //add object in list.
    List<Geo> geo = new List<Geo> {
                new Geo { Latitude=111,Longitude=222,Unit="jjj"},
                new Geo { Latitude = 112, Longitude = 223, Unit = "nnn" },
                new Geo { Latitude = 113, Longitude = 224, Unit = "kkk" }
            };

    //looping for print list data in console.
    foreach (var s in geo)
    {
        Console.WriteLine(s.Latitude);
    }
    
    Console.ReadLine();
}
public class Geo
{
    public int Latitude { get; set; }
    public int Longitude { get; set; }
    public string Unit { get; set; }
}}

Upvotes: 3

jie
jie

Reputation: 31

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

geo_tag() Redundant ()

Upvotes: 3

go..
go..

Reputation: 948

Something like this for example?

       List<geo_tag> abc = new List<geo_tag> {};
        abc.Add(new geo_tag(11, 112, "SSS"));


  public class geo_tag
{
    public int latitude { get; set; }
    public int longitude { get; set; }
    public string unit { get; set; }

    public geo_tag()
    {
    }
    public geo_tag(int latitude, int longitude, string unit)
    {
        this.latitude = latitude;
        this.longitude = longitude;
        this.unit = unit;
    }
}

Upvotes: 0

KamalaH
KamalaH

Reputation: 1391

Creating new object of Geo and feeding the data at the time of initialization

  List<Geo> geo = new List<Geo> {
                    new Geo { Latitude=111,Longitude=222,Unit="jjj"},
                    new Geo { Latitude = 112, Longitude = 223, Unit = "nnn" },
                    new Geo { Latitude = 113, Longitude = 224, Unit = "kkk" }
                };

Upvotes: -1

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Try This

List<geo_tag> abc = new List<geo_tag>();

geo_tag Model= new geo_tag();
Model.latitude =111;
Model.longitude =122;
Model.unit ="SSS";

abc.Add(Model);

Upvotes: 4

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); 

Upvotes: 15

Related Questions