8.5: C# Examples
- Page ID
- 10714
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Objects
// This program creates instances of the Temperature class to convert Celsius
// and Fahrenheit temperatures.
//
// References:
// https://www.mathsisfun.com/temperature-conversion.html
// https://en.wikibooks.org/wiki/C_Sharp_Programming
using System;
public class Objects
{
public static void Main(String[] args)
{
Temperature temp1 = new Temperature(celsius: 0);
Console.WriteLine("temp1.Celsius = " + temp1.Celsius.ToString());
Console.WriteLine("temp1.Fahrenheit = " + temp1.Fahrenheit.ToString());
Console.WriteLine("");
temp1.Celsius = 100;
Console.WriteLine("temp1.Celsius = " + temp1.Celsius.ToString());
Console.WriteLine("temp1.Fahrenheit = " + temp1.Fahrenheit.ToString());
Console.WriteLine("");
Temperature temp2 = new Temperature(fahrenheit: 0);
Console.WriteLine("temp2.Fahrenheit = " + temp2.Fahrenheit.ToString());
Console.WriteLine("temp2.Celsius = " + temp2.Celsius.ToString());
Console.WriteLine("");
temp2.Fahrenheit = 100;
Console.WriteLine("temp2.Fahrenheit = " + temp2.Fahrenheit.ToString());
Console.WriteLine("temp2.Celsius = " + temp2.Celsius.ToString());
}
}
// This class converts temperature between Celsius and Fahrenheit.
// It may be used by assigning a value to either Celsius or Fahrenheit
// and then retrieving the other value, or by calling the ToCelsius or
// ToFahrenheit methods directly.
public class Temperature
{
double _celsius;
double _fahrenheit;
public double Celsius
{
get
{
return _celsius;
}
set
{
_celsius = value;
_fahrenheit = ToFahrenheit(value);
}
}
public double Fahrenheit
{
get
{
return _fahrenheit;
}
set
{
_fahrenheit = value;
_celsius = ToCelsius(value);
}
}
public Temperature(double? celsius = null, double? fahrenheit = null)
{
if (celsius.HasValue)
{
this.Celsius = Convert.ToDouble(celsius);
}
if (fahrenheit.HasValue)
{
this.Fahrenheit = Convert.ToDouble(fahrenheit);
}
}
public double ToCelsius(double fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
public double ToFahrenheit(double celsius)
{
return celsius * 9 / 5 + 32;
}
}
Output
temp1.Celsius = 0 temp1.Fahrenheit = 32 temp1.Celsius = 100 temp1.Fahrenheit = 212 temp2.Fahrenheit = 0 temp2.Celsius = -17.7777777777778 temp2.Fahrenheit = 100 temp2.Celsius = 37.7777777777778
References
- Wikiversity: Computer Programming

