6

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

2
  • 4
    I believe .NET 2.0 did not support inline object initialization. You need to use later version of .NET and C# or create an object of geo_tag separately and then add it the list.
    – Chetan
    Oct 1, 2018 at 9:56
  • 2
    Are you sure you are using .net 2 (2005) ? And if so, why?
    – bommelding
    Oct 1, 2018 at 10:13

6 Answers 6

14

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

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);
0
3
List<geo_tag> abc = new List<geo_tag>();
abc.Add(new geo_tag { latitude = 111, longitude = 122, unit = "SSS" }); 

geo_tag() Redundant ()

1
  • 1
    Can you please add more explaining words to your code? Apr 8, 2021 at 11:35
2
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; }
}}
1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach? May 2, 2022 at 18:33
0

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;
    }
}
3
  • 1
    We don´t really like code-only answers. Explain what the idea is and how to use it. Oct 1, 2018 at 10:18
  • @HimBromBeere yes ı'know. :)
    – go..
    Oct 1, 2018 at 10:26
  • 1
    There´s still not really an explanation or anything meaningul. Oct 1, 2018 at 10:32
-1

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" }
                };
1
  • 1
    Why is this supposed to help?
    – bommelding
    Oct 1, 2018 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.