Click here to Skip to main content
14,240,044 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to understand what I am missing! I would like to be able to convert a string variable in to something readable in a List<> or IList<>?
Thank You in advance for any assistance in furthering my understanding :)

What I have tried:

<pre lang="c#"><pre>using System;
using System.Collections.Generic;

namespace TestListConsoleApp1
    class Program

        static void Main(string[] args)

            // Variable Type defined
            string fName, lName;

            // Variable assignment
            Console.WriteLine("Enter a first name: ");
            fName = Console.ReadLine();
            Console.WriteLine("Enter a last name: ");
            lName = Console.ReadLine();
            // Create a list of First and Last names
            List<Names> name = new List<Names>();

            //Add a First Name and Last Name to the list

    public class Names

        public string firstName { get; set; }
        public string lastName { get; set; }

Updated 11-Jul-19 19:08pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Okay answered my own question... int.Parse
Patrice T 12-Jul-19 0:44am
Accept your own solution to close the question.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Okay answered my own question... int.Parse

Instead of using int.Parse, I'd strongly suggest you use int.TryParse[^] - Parse will throw an exception (which will crash your app) is the user input is not a valid integer, TryParse won't. Since users have six thumbs on each hand, it's best not to rely on them always typing correctly!
int value;
   Console.Write("Enter a number: ");
   string userInput = Console.ReadLine();
   } while (!int.TryParse(userInput, out value));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100