Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to initialize objects and collections in C # 3.0

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize C#3.0 object and collection initialization". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian to study and learn "how to realize C#3.0 object and collection initialization" together.

Point class

public class Point { public int X { get; set; } public int Y { get; set; } }

Object class initialization can be defined as follows

Point p = new Point { X = 3, Y = 99 };

If it is a collection initialization, it mainly inherits System.Collections.Generic.IEnumerable

< T>

And there is a common method Add that initializes the collection.

An example of collection initialization is as follows

List

< Point>

Square = new List

< Point>

{ new Point { X=0, Y=5 }, new Point { X=5, Y=5 }, new Point { X=5, Y=0 }, new Point { X=0, Y=0 } };

Complete example source code

class Program { static List

< Customer>

CreateCustomers() { return new List

< Customer>

{ new Customer(1) { Name = "Alex Roland", City = "Berlin" }, new Customer(2) { Name = "Oliver Cox", City = "Marseille" }, new Customer(3) { Name = "Maurice Taylor", City = "London" }, new Customer(4) { Name = "Phil Gibbins", City = "London" }, new Customer(5) { Name = "Tony Madigan", City = "Torino" }, new Customer(6) { Name = "Elizabeth A. Andersen", City = "Portland" }, new Customer(7) { Name = "Justin Thorp", City = "London" }, new Customer(8) { Name = "Bryn Paul Dunton", City = "Portland" } }; } static void Main(string[] args) { List

< Customer>

customers = CreateCustomers(); Console.WriteLine("Customers:\n"); foreach (Customer c in customers) Console.WriteLine(c); } Thank you for reading, the above is the content of "C#3.0 object and collection initialization how to achieve", after the study of this article, I believe that everyone has a deeper understanding of how to achieve C#3.0 object and collection initialization, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report