Create struct for Collection with new syntax in CSharp

Description

The following code shows how to create struct for Collection with new syntax.

Example


     /*w w w .j  a  v  a  2  s  .com*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


      public struct MyAddress
      {
         public String Name;
         public String Company;
         public String Address1;
         public String Address2;
         public String City;
         public String State;
         public String ZIP;
      }
public class MainClass{

   public static void Main(string[] args){   
         List<MyAddress> AddressCollection = new List<MyAddress>
         {
            new MyAddress{
               Name = "S",
               Address1 = "1234 ",
               Address2 = "3",
               City = "Anywhere",
               State = "WA",
               ZIP = "12345"
            },
            new MyAddress
            {
               Name = "M",
               Company = "Us",
               Address1 = "21st Street",
               City = "City",
               State = "MI",
               ZIP = "99999"
            }
         };

         foreach (MyAddress ThisAddress in AddressCollection){
            Console.WriteLine(
               ThisAddress.Name + "\r\n" +
               ThisAddress.Company + "\r\n" +
               ThisAddress.Address1 + "\r\n" +
               ThisAddress.Address2 + "\r\n" +
               ThisAddress.City + ", " +
               ThisAddress.State + "     " +
               ThisAddress.ZIP);
            
         }


   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var