Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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; }

    }
}
Posted
Updated 11-Jul-19 19:08pm

Okay answered my own question... int.Parse
 
Share this answer
 
Comments
Patrice T 12-Jul-19 0:44am    
Accept your own solution to close the question.
Quote:
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!
C#
int value;
do 
   {
   Console.Write("Enter a number: ");
   string userInput = Console.ReadLine();
   } while (!int.TryParse(userInput, out value));
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900