visual c#
C#
int a = 42;
int b = 119;
int c = a + b;
Console.WriteLine(c);
Console.ReadKey();
class Calculator
{
public static double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.
// Use a switch statement to do the math.
switch (op)
{
case "a":
result = num1 + num2;
break;
case "s":
result = num1 - num2;
break;
case "m":
result = num1 * num2;
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
}
break;
// Return text for an incorrect option entry.
default:
break;
}
return result;
}
}
using System;
namespace Calculator
{
class Calculator
{
public static double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.
// Use a switch statement to do the math.
switch (op)
{
case "a":
result = num1 + num2;
break;
case "s":
result = num1 - num2;
break;
case "m":
result = num1 * num2;
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
}
break;
// Return text for an incorrect option entry.
default:
break;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
bool endApp = false;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
while (!endApp)
{
// Declare variables and set to empty.
string numInput1 = "";
string numInput2 = "";
double result = 0;
// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}
// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string op = Console.ReadLine();
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("\n"); // Friendly linespacing.
}
return;
}
}
}
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
// Declare variables and then initialize to zero.
int num1 = 0; int num2 = 0;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
// Ask the user to type the first number.
Console.WriteLine("Type a number, and then press Enter");
num1 = Convert.ToInt32(Console.ReadLine());
// Ask the user to type the second number.
Console.WriteLine("Type another number, and then press Enter");
num2 = Convert.ToInt32(Console.ReadLine());
// Ask the user to choose an option.
Console.WriteLine("Choose an option from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
// Use a switch statement to do the math.
switch (Console.ReadLine())
{
case "a":
Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
break;
case "s":
Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
break;
case "m":
Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
break;
case "d":
Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
break;
}
// Wait for the user to respond before closing.
Console.Write("Press any key to close the Calculator console app...");
Console.ReadKey();
}
}
}
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
bool endApp = false;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
while (!endApp)
{
// Declare variables and set to empty.
string numInput1 = "";
string numInput2 = "";
double result = 0;
// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}
// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string op = Console.ReadLine();
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
// Wait for the user to respond before closing.
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("\n"); // Friendly linespacing.
}
return;
}
}
Also in C#:
- Title
- c# communicate with arduino
- Category
- C#
- Title
- c# foreach namevaluecollection
- Category
- C#
- Title
- background color with opacity
- Category
- C#
- Title
- while loop c#
- Category
- C#
- Title
- how to clear console in c#
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- vb.net tostring numeric format string
- Category
- C#
- Title
- c# generic abstract method
- Category
- C#
- Title
- roulette algorithm genetic algorithm
- Category
- C#
- Title
- unity serializefield
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- unity rotation
- Category
- C#
- Title
- select a whole row out of a 2d array C#
- Category
- C#
- Title
- variables
- Category
- C#
- Title
- check if number is even or odd c#
- Category
- C#
- Title
- json serialize object capitalization config
- Category
- C#
- Title
- how to record number of times using application in c#
- Category
- C#
- Title
- c# find duplicates in list of strings
- Category
- C#
- Title
- c# using get set methods
- Category
- C#
- Title
- get out of foreach statement c#
- Category
- C#
- Title
- unity gizmo draw line
- Category
- C#
- Title
- socket in c#
- Category
- C#
- Title
- C# Unknown column 'FundAllocation' in 'field list
- Category
- C#
- Title
- unity set object scale
- Category
- C#
- Title
- asign only common fields in c# object
- Category
- C#
- Title
- convert generic to type c#
- Category
- C#
- Title
- convert double to currency c#
- Category
- C#
- Title
- get number of sundays in a month c#
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- c# while loop
- Category
- C#
- Title
- c# discord bot
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- c# singleton
- Category
- C#
- Title
- how to make an object move in unity
- Category
- C#
- Title
- create object in c#
- Category
- C#
- Title
- c# sort for loop
- Category
- C#
- Title
- how to say or in c#
- Category
- C#
- Title
- c# check if argument null
- Category
- C#
- Title
- unity main texture not working
- Category
- C#
- Title
- verifyusertokenasync password reset token
- Category
- C#
- Title
- c# postfix increment operator overload
- Category
- C#
- Title
- c# delegate return value invoke
- Category
- C#
- Title
- c# duplicate object instance
- Category
- C#
- Title
- c# sql duplicate key exception
- Category
- C#
- Title
- check if network is available c#
- Category
- C#
- Title
- loops in coding
- Category
- C#
- Title
- c# storing value in session
- Category
- C#
- Title
- unity 2d platformer movement script c#
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- unity input get axis
- Category
- C#
- Title
- c# listview
- Category
- C#
- Title
- c# get motherboard id
- Category
- C#
- Title
- C# move form without border
- Category
- C#
- Title
- unity set list of strings
- Category
- C#
- Title
- unity 2d detect click on sprite
- Category
- C#
- Title
- escape double quotes c#
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- c# build string out of list of strings
- Category
- C#
- Title
- c# object clone
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- optional parameter get request c#
- Category
- C#
- Title
- unity how to tell when a gameobject is colliding
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- date time heutiges datum
- Category
- C#
- Title
- c# new dictionary linq
- Category
- C#
- Title
- split string
- Category
- C#
- Title
- c# mixed multidimensional array
- Category
- C#
- Title
- c# two dimensional array
- Category
- C#
- Title
- unity print to console
- Category
- C#
- Title
- c# group array based on first character
- Category
- C#
- Title
- .net core check if linux
- Category
- C#
- Title
- unity rotate around point
- Category
- C#
- Title
- defualtsize UWP c#
- Category
- C#
- Title
- how to deactivate objects through scripts in unity
- Category
- C#
- Title
- C# int to hex
- Category
- C#
- Title
- if entity.is Transient() Update Mvc 5 c#
- Category
- C#
- Title
- void start
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- c# list remove item based on property duplicate
- Category
- C#
- Title
- c# 2 timespan return yesterday
- Category
- C#
- Title
- how to detect if a key is pressed in c#
- Category
- C#
- Title
- c# region tag
- Category
- C#
- Title
- how to get component in unity c#
- Category
- C#
- Title
- c# verify in class exist in list
- Category
- C#
- Title
- c# join array
- Category
- C#
- Title
- example HttpClient c# Post
- Category
- C#
- Title
- httpclient soap request c#
- Category
- C#
- Title
- How to get the world position of the edge of an object?
- Category
- C#
- Title
- dont destroy on load unity
- Category
- C#
- Title
- How to solve error in ExecuteNonQuery() in asp.net
- Category
- C#
- Title
- unity rb.addexplosionforce 2d
- Category
- C#
- Title
- how to get value from object in c#
- Category
- C#
- Title
- format string to date only c#
- Category
- C#
- Title
- what is the or symbol in C#
- Category
- C#
- Title
- unity find gameobject with layer
- Category
- C#
- Title
- blazor wasm routable page in separate project
- Category
- C#
- Title
- how to declare 2d array in c#
- Category
- C#
- Title
- unity get decimal part of float
- Category
- C#
- Title
- c# convert Unix time in seconds to datetime
- Category
- C#