Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

C Sharp List Examples

C# List Examples
var duplicatePets = listExamples.ReturnListOfPets()
                                            .GroupBy(x => x.StringValue)
                                            .Where(y => y.Count() > 1)
                                            .Select(z => z.Key).ToList();
public List<int> SortReturnListOfInts()
        {
            ListOfIntsSixToTen newList = new ListOfIntsSixToTen();

            var sortList = newList.ReturnListOfInts();
            sortList.Sort();
 
            return sortList;
        }
public class PetList
    {
        public PetList(string stringId, string stringValue)
        {
            StringId        = stringId;
            StringValue     = stringValue;
        }
        public string StringId      { get;}
        public string StringValue   { get;}
    }

public List<PetList> ReturnListOfPets()
        {
            List<PetList> listOfPets = new List<PetList>
            {
                new PetList("Dog", "German Shepherd"),
                new PetList("Dog", "German Shepherd"),
                new PetList("Cat", "Russian Blue"),
                new PetList("Cat", "Persian Cat"),
                new PetList("Cat", "Persian Cat")
            };

            return listOfPets;

        }
public List<int> ReturnListOfInts()
        {
            List<int> listOfInts = new List<int>
            {
                1,
                2,
                3,
                4,
                5
            };

            return listOfInts;
        }
public class ListOfIntsSixToTen
    {
        public List<int> ReturnListOfInts()
        {
            List<int> listOfInts = new List<int>
            {
                10,
                6,
                8,
                7,
                9
            };

            return listOfInts;
        }
    }
public List<int> ReturnListOfIntsJoin()
        {
            ListOfIntsSixToTen newList      = new ListOfIntsSixToTen();
            List<int> intsInAnotherClass    = newList.ReturnListOfInts();

            List<int> intsInThisClass = new List<int>
            {
                1,
                2,
                3,
                4,
                5
            };

            intsInThisClass.AddRange(intsInAnotherClass);

            return intsInThisClass;
        }
public List<string> ReturnListOfStrings()
        {
            List<string> listOfStrings = new List<string>
            {
                "One",
                "Two",
                "Three",
                "Four",
                "Five"
            };

            return listOfStrings;
        }
using System.Collections.Generic;

namespace Web.Helper.ListHelper
{
    public class PetList
    {
        public PetList(string stringId, string stringValue)
        {
            StringId        = stringId;
            StringValue     = stringValue;
        }
        public string StringId      { get;}
        public string StringValue   { get;}
    }


    public class ListOfIntsSixToTen
    {
        public List<int> ReturnListOfInts()
        {
            List<int> listOfInts = new List<int>
            {
                10,
                6,
                8,
                7,
                9
            };

            return listOfInts;
        }
    }

    public class ListExamples
    {

        public List<int> ReturnListOfInts()
        {
            List<int> listOfInts = new List<int>
            {
                1,
                2,
                3,
                4,
                5
            };

            return listOfInts;
        }

        public List<int> ReturnListOfIntsJoin()
        {
            ListOfIntsSixToTen newList      = new ListOfIntsSixToTen();
            List<int> intsInAnotherClass    = newList.ReturnListOfInts();

            List<int> intsInThisClass = new List<int>
            {
                1,
                2,
                3,
                4,
                5
            };

            intsInThisClass.AddRange(intsInAnotherClass);

            return intsInThisClass;
        }

        public List<int> SortReturnListOfInts()
        {
            ListOfIntsSixToTen newList = new ListOfIntsSixToTen();

            var sortList = newList.ReturnListOfInts();
            sortList.Sort();
 
            return sortList;
        }



        public List<string> ReturnListOfStrings()
        {
            List<string> listOfStrings = new List<string>
            {
                "One",
                "Two",
                "Three",
                "Four",
                "Five"
            };

            return listOfStrings;
        }

        public List<PetList> ReturnListOfPets()
        {
            List<PetList> listOfPets = new List<PetList>
            {
                new PetList("Dog", "German Shepherd"),
                new PetList("Dog", "German Shepherd"),
                new PetList("Cat", "Russian Blue"),
                new PetList("Cat", "Persian Cat"),
                new PetList("Cat", "Persian Cat")
            };

            return listOfPets;

        }
    }
}
using System.Collections.Generic;
using Web.Helper.ListHelper;

namespace Web.Model.ListViewModel
{
    public class ExampleOfListViewModel
    {
        public List<PetList> ListOfPets                 { get; set; }
        public List<string> ListOfPetsDuplicates       { get; set; }
        public List<int> ListOfInts                     { get; set; }
        public List<string> ListOfStrings               { get; set; }
        public List<int> ListOfIntsJoined               { get; set; }
        public List<int> ListOfIntsSorted               { get; set; }
    }
}
using System.Linq;
using System.Web.Mvc;
using Web.Helper.ListHelper;
using Web.Model.ListViewModel;

namespace Web.UI.Controllers
{
    public class ListExamplesController : Controller
    {
        public ActionResult Index()
        {
            ListExamples listExamples = new ListExamples();

            var duplicatePets = listExamples.ReturnListOfPets()
                                            .GroupBy(x => x.StringValue)
                                            .Where(y => y.Count() > 1)
                                            .Select(z => z.Key).ToList();

            var model = new ExampleOfListViewModel
            {
                ListOfPets              = listExamples.ReturnListOfPets(),
                ListOfInts              = listExamples.ReturnListOfInts(),
                ListOfStrings           = listExamples.ReturnListOfStrings(),
                ListOfIntsJoined        = listExamples.ReturnListOfIntsJoin(),
                ListOfPetsDuplicates    = duplicatePets,
                ListOfIntsSorted        = listExamples.SortReturnListOfInts()
            };
            return View("~/Views/Home/ListExamples.cshtml",model);
        }
    }
}
@model Web.Model.ListViewModel.ExampleOfListViewModel
@{
    ViewBag.Title = "ListExamples";
}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2>List Examples</h2>
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <p><strong>List of pets</strong></p>
            <ul>
                @foreach (var pet in Model.ListOfPets)
                {
                    <li>@pet.StringId <br/> @pet.StringValue <hr/></li>
                }
            </ul>
        </div>
        <div class="col-md-2">
            <p><strong>List of Ints</strong></p>
            <ul>
                @foreach (var intValue in Model.ListOfInts)
                {
                    <li>@intValue</li>
                }
            </ul>
        </div>
        <div class="col-md-2">
            <p><strong>List of ints joined</strong></p>
            <ul>
                @foreach (var intValue in Model.ListOfIntsJoined)
                {
                    <li>@intValue</li>
                }
            </ul>
        </div>
        <div class="col-md-2">
            <p><strong>List of strings</strong></p>
            <ul>
                @foreach (var stringValue in Model.ListOfStrings)
                {
                    <li>@stringValue</li>
                }
            </ul>
        </div>
        <div class="col-md-2">
            <p><strong>List of duplicate pets</strong></p>
            <ul>
                @foreach (var pet in Model.ListOfPetsDuplicates)
                {
                    <li>@pet</li>
                }
            </ul>
        </div>
        <div class="col-md-2">
            <p><strong>List of ints sorted</strong></p>
            <ul>
                @foreach (var intsSorted in Model.ListOfIntsSorted)
                {
                    <li>@intsSorted</li>
                }
            </ul>
        </div>
    </div>
</div>

Please enter your comment.