我多年没有使用过静态类型的语言,并且自己设定了使用C#加速的任务.我正在使用我常用的技巧来完成15个练习http://www.jobsnake.com/seek/articles/index.cgi?openarticle&8533作为我的第一个任务.
我刚刚完成了第二个Fibonacci任务,它没有花很长时间并且工作得很好,但在我看来看起来很难看,而且我确信可以通过更少的优雅代码来实现.
我通常喜欢通过与已经知道他们正在做什么的人进行结对编程来学习,但是这个选项今天不对我开放,所以我希望在这里发布将是下一个最好的事情.
所以对于所有C#Jedi来说,如果你要重构下面的代码,它会是什么样子?
using System;
using System.Collections;
namespace Exercises
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Find all fibinacci numbers between:");
int from = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("And:");
int to = Convert.ToInt32(Console.ReadLine());
Fibonacci fibonacci = new Fibonacci();
fibonacci.PrintArrayList(fibonacci.Between(from, to));
}
}
class Fibonacci
{
public ArrayList Between(int from, int to)
{
int last = 1;
int penultimate = 0;
ArrayList results = new ArrayList();
results.Add(penultimate);
results.Add(last);
while(last<to)
{
int fib = last + penultimate;
penultimate = …Run Code Online (Sandbox Code Playgroud)