Add new item to List object C#

后端 未结 4 1383
灰色年华
灰色年华 2021-01-16 09:33

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

List abc = new List();
abc.Add(new geo_tag() { latitude = 111, longitude = 122, un         


        
相关标签:
4条回答
  • 2021-01-16 10:04

    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 讨论(0)
  • 2021-01-16 10:06

    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" }
                    };
    
    0 讨论(0)
  • 2021-01-16 10:10

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-16 10:18

    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 讨论(0)
提交回复
热议问题