

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An example of a simple c# program that uses the console to take user input and convert data types. The main method is explained, as well as the use of integer and float data types and the parse method for string to data type conversion.
Typology: Schemes and Mind Maps
1 / 2
This page cannot be seen from the preview
Don't miss anything!


// The namespaces are used to organize classes in a project. The namespace declaration is a collection of classes. In my case, the namespace is DataTypesApp contains the class DataTypesProgram. // The using keyword is used to access the classes available in a namespace. using System; namespace DataTypesApp { class DataTypesProgram { static void Main(string[] args) // It is a keyword and used to specify the main method Every C# application contains one (1) Main method. The Main method is the starting point of every application, and it contains the statements that will be executed. In the sample program, we use the; int apple; // Integer - is a data type used to represent real numbers that do not have fractional values float price; // a data type composed of a number that is not an integer
this is where we input the number of apples apple = int.Parse(Console.ReadLine());// this part is when it used to read the next line of characters from the standard input and again you can see the Parse where in // parsing is the process where a string of commands – usually a program – is separated into more easily processed components, which are analyzed for correct syntax and then attached to tags that define each component. Console.Write("Enter the total price of " + apple + " apple(s): "); price = float.Parse(Console.ReadLine()); // float. Parse method uses your current culture settings by default int newVariable = (int)price; Console.WriteLine("The total price of " + apple + " apple(s) is " + price); // Console.WriteLine() is a method that works with Console applications. It prints the string argument to the console screen Console.WriteLine("The value of original price is " + price); Console.WriteLine("The value of converted price is " + (int)price); Console.WriteLine("Press any key to exit......"); Console.ReadKey();