Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,235 questions

56,138 answers

573 users

How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public int Zip { get; set; }
}

enum Status
{
    Active,
    Inactive,
    Unknown
}

class Person
{
    public Address HomeAddress { get; set; }
    public int[] FavoriteNumbers { get; set; }
    public string? Nickname { get; set; }
    public DateTime BirthDate { get; set; }
    public string Notes { get; set; }
    public string? MiddleName { get; set; }
    public List<string> Hobbies { get; set; }
    public Status Status { get; set; }

    public Person()
    {
        HomeAddress = new Address();
        FavoriteNumbers = new int[5];
        Nickname = null;
        BirthDate = new DateTime(1990, 1, 1);
        Notes = "No notes";
        MiddleName = null;
        Hobbies = new List<string>();
        Status = Status.Unknown;
    }
}

class Program
{
    static void PrintPerson(Person p)
    {
        Console.WriteLine($"Address: {p.HomeAddress.Street}, {p.HomeAddress.City} {p.HomeAddress.Zip}");

        Console.Write("Favorite Numbers: ");
        foreach (var n in p.FavoriteNumbers)
            Console.Write(n + " ");
        Console.WriteLine();

        Console.WriteLine("Nickname: " + (p.Nickname ?? "(none)"));

        Console.WriteLine("Birth Date: " + p.BirthDate.ToString("yyyy-MM-dd"));

        Console.WriteLine("Notes: " + p.Notes);

        Console.WriteLine("Middle Name: " + (p.MiddleName ?? "(none)"));

        Console.Write("Hobbies: ");
        foreach (var h in p.Hobbies)
            Console.Write(h + ", ");
        Console.WriteLine();

        Console.WriteLine("Status: " + p.Status);
    }

    static void Main()
    {
        Person p = new Person();

        p.HomeAddress.Street = "Fifth Avenue";
        p.HomeAddress.City = "New York";
        p.HomeAddress.Zip = 423900;

        p.FavoriteNumbers = new[] { 7, 13, 21, 42, 99 };

        p.Nickname = "Orion";

        p.BirthDate = new DateTime(1026, 6, 15);

        p.Notes = "Voss likes C/C++ and structs";

        p.MiddleName = "Pulse‑9";

        p.Hobbies.Add("Coding");
        p.Hobbies.Add("Dreaming");
        p.Hobbies.Add("Walking");
        p.Hobbies.Add("Sci-Fi Movies");

        p.Status = Status.Active;

        PrintPerson(p);
    }
}


/*
run:

Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 99 
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse?9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
Status: Active

*/

 



answered Sep 3 by avibootz

Related questions

...