如果之前有人问过这个问题,我会道歉,但我是编程新手。
问题: 每当我创建属性时,我都会收到一条消息,说明我应该将其转换为自动属性。
我的程序运行所以我应该担心这个吗?
using System;
namespace ConsoleApplication12
{
struct Calculator
{
// Properties for returning addition numbers
private static long _add1, _add2;
private static long _addtotal;
public static long AddNumberOne
{
get { return _add1; } set { _add1 = value; }
}
public static long AddNumberTwo
{
get { return _add2; } set { _add2 = value; }
}
// Properties for returning multiplication numbers
private static long _mul1, _mul2;
private static long _multiplytotal;
public static long MulNumberOne
{
get { return _mul1; } set { _mul1 = value; }
}
public static long MulNumberTwo
{
get { return _mul2; } set { _mul2 = value; }
}
// Properties for returning division numbers
private static double _div1, _div2;
private static double _dividetotal;
public static double DivNumberOne
{
get { return _div1; } set { _div1 = value; }
}
public static double DivNumberTwo
{
get { return _div2; } set { _div2 = value; }
}
// Properties for returning minus numbers
private static double _min1, _min2;
private static double _minustotal;
public static double MinNumberOne
{
get { return _min1; } set { _min1 = value; }
}
public static double MinNumberTwo
{
get { return _min2; } set { _min2 = value; }
}
// Property for returning program run state
private static bool _run = true;
public static bool RunProgram
{
get { return _run; } set { _run = value; }
}
// Properties for returning total after calculation is made
public static long TotalAddition
{
get { return _addtotal; } set { _addtotal = value; }
}
public static long TotalMultiply
{
get { return _multiplytotal; } set { _multiplytotal = value; }
}
public static double TotalDivide
{
get { return _dividetotal; } set { _dividetotal = value; }
}
public static double TotalMinus
{
get { return _minustotal; } set { _minustotal = value; }
}
// Methods for calculating total
public static long AddCalculation(long a, long b)
{
TotalAddition = a + b;
return TotalAddition;
}
public static long MultiplyCalculation(long a, long b)
{
TotalMultiply = a*b;
return TotalMultiply;
}
public static double DivideCalculation(double a, double b)
{
TotalDivide = a/b;
return TotalDivide;
}
public static double MinusCalculation(double a, double b)
{
TotalMinus = a - b;
return TotalMinus;
}
// Properties for user input
private static string _input;
private static long _inputConv;
public static string InputOption
{
get { return _input; }
set { _input = value; }
}
public static long InputConversion
{
get { return _inputConv; }
set { _inputConv = value; }
}
public static void DisplayInstructionMenuAndSelectCalculatioType()
{
Console.WriteLine("What operation would you like to carry out?\n");
Console.WriteLine("1. Addition\n" +
"2. Multiplication\n" +
"3. Subtraction\n" +
"4. Division");
InputOption = Console.ReadLine();
InputConversion = Convert.ToInt64(InputOption);
}
public static void UserInputForAdditionCalculation()
{
Console.WriteLine("*******ADDITION*******");
Console.WriteLine("Enter number 1: ");
InputOption = Console.ReadLine();
AddNumberOne = Convert.ToInt64(InputOption);
Console.WriteLine("Enter number 2: ");
InputOption = Console.ReadLine();
AddNumberTwo = Convert.ToInt64(InputOption);
TotalAddition = AddCalculation(AddNumberOne, AddNumberTwo);
Console.WriteLine("\n\nResult: {0} + {1} = {2}", AddNumberOne, AddNumberTwo, TotalAddition);
Console.Write("Another calculation? YES/NO: ");
InputOption = Console.ReadLine();
}
public static void UserInputForMultiplyCalculation()
{
Console.WriteLine("*******MULTIPLY*******");
Console.WriteLine("Enter number 1: ");
InputOption = Console.ReadLine();
MulNumberOne = Convert.ToInt64(InputOption);
Console.WriteLine("Enter number 2: ");
InputOption = Console.ReadLine();
MulNumberTwo = Convert.ToInt64(InputOption);
TotalMultiply = MultiplyCalculation(MulNumberOne, MulNumberTwo);
Console.WriteLine("\n\nResult: {0} x {1} = {2}", MulNumberOne, MulNumberTwo, TotalMultiply);
Console.Write("Another calculation? YES/NO: ");
InputOption = Console.ReadLine();
}
public static void UserInputForSubtractCalculation()
{
Console.WriteLine("*******SUBTRACT*******");
Console.WriteLine("Enter number 1: ");
InputOption = Console.ReadLine();
MinNumberOne = Convert.ToInt64(InputOption);
Console.WriteLine("Enter number 2: ");
InputOption = Console.ReadLine();
MinNumberTwo = Convert.ToInt64(InputOption);
TotalMinus = MinusCalculation(MinNumberOne, MinNumberTwo);
Console.WriteLine("\n\nResult: {0} - {1} = {2}", MinNumberOne, MinNumberTwo, TotalMinus);
Console.Write("Another calculation? YES/NO: ");
InputOption = Console.ReadLine();
}
public static void UserInputForDivisionCalculation()
{
Console.WriteLine("*******DIVISION*******");
Console.Write("Enter number 1: ");
InputOption = Console.ReadLine();
DivNumberOne = Convert.ToInt64(InputOption);
Console.Write("Enter number 2: ");
InputOption = Console.ReadLine();
DivNumberTwo = Convert.ToInt64(InputOption);
TotalDivide = DivideCalculation(DivNumberOne, DivNumberTwo);
Console.WriteLine("\n\nResult: {0} / {1} = {2}", DivNumberOne, DivNumberTwo, TotalDivide);
Console.Write("Another calculation? YES/NO: ");
InputOption = Console.ReadLine();
}
public static void DisplayMenuOrExitProgram()
{
switch (InputOption)
{
case "YES":
DisplayInstructionMenuAndSelectCalculatioType();
break;
case "NO":
RunProgram = false;
break;
default:
RunProgram = false;
break;
}
}
public static void SwitchCalculationsAndDisplayResults()
{
while (RunProgram)
{
switch (InputConversion)
{
case 1:
UserInputForAdditionCalculation();
DisplayMenuOrExitProgram();
break;
case 2:
UserInputForMultiplyCalculation();
DisplayMenuOrExitProgram();
break;
case 3:
UserInputForSubtractCalculation();
DisplayMenuOrExitProgram();
break;
case 4:
UserInputForDivisionCalculation();
DisplayMenuOrExitProgram();
break;
default:
Console.WriteLine("Something went wrong, sorry");
break;
}
}
}
}
}
答案 0 :(得分:7)
您可以创建名为Auto-Properties
的较短的属性:
public static long MulNumberOne { get; set; }
您可以保存几行代码,但编译器将在编译时生成相同的IL。
您的类/结构的属性可以缩短为以下内容:
struct Calculator
{
// Properties for returning addition numbers
public static long AddNumberOne { get; set; }
public static long AddNumberTwo { get; set; }
// Properties for returning multiplication numbers
public static long MulNumberOne { get; set; }
public static long MulNumberTwo { get; set; }
// Properties for returning division numbers
public static double DivNumberTwo { get; set; }
// Properties for returning minus numbers
public static double MinNumberOne { get; set; }
public static double MinNumberTwo { get; set; }
// Property for returning program run state
public static bool RunProgram { get; set; }
// Properties for returning total after calculation is made
public static long TotalAddition { get; set; }
public static long TotalMultiply { get; set; }
public static double TotalDivide { get; set; }
public static double TotalMinus { get; set; }
// Properties for user input
public static string InputOption { get; set; }
public static long InputConversion { get; set; }
// METHODS
}
这将在编译后生成相同的输出,但要更好地阅读IMO
除了_add1
之外,您还需要在类中使用AddNumberOne
,这也会调用getter方法并且速度稍慢(仅在每秒100k次调用时才有效)。
答案 1 :(得分:0)
这意味着你不会使用背景而你不需要它,你的属性可以
public static double MinNumberOne
{
get; set;
}
编译将为您创建后退字段。
你不应该对此有所了解,auto-prop是一种语法糖。
答案 2 :(得分:0)
你不应该担心它。自动属性与您现在使用的属性相同,只是更短的方式。当get和set访问器未更改时,将使用它们。 这是您的代码中的示例:
public static double MinNumberOne
{
get { return _min1; } set { _min1 = value; }
}
这是怎么回事。这是一个自动财产。
public static double MinNumberOne { get; set; }