这是我简化的双重递归方法.它没有任何用处,但说明了所需的递归调用:
void Main()
{
Test(2, 3, 4);
}
int n1 = 0;
int n2 = 0;
void Test(int i1, int i2, int v)
{
if (v == 0)
{
(n1 + n2).Dump();
}
else
{
n1 = i1 + 10;
n2 = i2 + 20;
Test(n1, n2, v - 1);
Test(n2, n1, v - 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法想象如何将其作为一个循环来编写,看看性能是否有所改善.
我已经纠正了明显错误的例子.
理想我想提供一系列点,并在每个点(从第二点开始)以直角绘制一条线.
每条线的方向会交替出现,所以如果我碰巧绘制了一条由6个点组成的曲线,则从第二个点开始绘制每个点的给定长度线,即曲线交替边上的5个额外线,有点像毛毛虫交替腿.
(我知道线条不会与曲线完全成直角,而是与曲线上任意两点形成的直线成直角).
我已将代码从javascript翻译成c#,可以通过访问http://fractal.qfox.nl/dragon.js上的这个优秀演示找到
我的翻译是为了在点击按钮时只产生一条龙,但我想我错过了我的版本.
有关更多信息,请参阅维基百科文章:Dragon Curve.
不完整的龙分形输出:
码:
public partial class MainPage : UserControl
{
PointCollection pc;
Int32[] pattern = new Int32[] { 1, 1, 0, 2, 1, 0, 0, 3 };
Int32[] position = new Int32[] { 0, 0, 0, 0, 0, 0, 0, 0 };
Boolean toggle;
Char r = default(Char);
Int32 distance = 10; // line length
Int32 step = 100; // paints per step
Int32 skip = 10; // folds per paint
Double x …Run Code Online (Sandbox Code Playgroud) 例如,我可以做类似的事情:
switch (myString)
case "rectangle":
o = new rect();
break;
case "ellipse"
etc...
Run Code Online (Sandbox Code Playgroud)
但是我怎么不做上面的事情,即只有一行代码直接从字符串中获取对象.想象一下,例如,一个按钮以及用户点击它时所说的任何内容,它会显示所显示的文本并从中创建一个对象.
我简化了这一点只是为了展示展示问题所需要的东西 - 即虽然8个项目明显在列表框中,但它们没有内容,即"名称"不显示,它们只是空白.如果我在设置ItemSource之后设置断点,我可以看到源已经正确填充了集合,所以我假设我的xaml必定是错误的.这是代码和xaml:
public partial class MainPage : UserControl
{
private ObservableCollection<ToolboxItem> ToolboxItems;
public MainPage()
{
InitializeComponent();
InitToolboxItems();
lstToolbox.ItemsSource = ToolboxItems;
}
private void InitToolboxItems()
{
ToolboxItems = new ObservableCollection<ToolboxItem>();
ToolboxItems.Add(new ToolboxItem(name: "Item1"));
ToolboxItems.Add(new ToolboxItem(name: "Item2"));
ToolboxItems.Add(new ToolboxItem(name: "Item3"));
ToolboxItems.Add(new ToolboxItem(name: "Item4"));
ToolboxItems.Add(new ToolboxItem(name: "Item5"));
ToolboxItems.Add(new ToolboxItem(name: "Item6"));
ToolboxItems.Add(new ToolboxItem(name: "Item7"));
ToolboxItems.Add(new ToolboxItem(name: "Item8"));
}
public struct ToolboxItem
{
public String Name;
public ToolboxItem(String name) { Name = name; }
}
}
<Grid x:Name="LayoutRoot" Background="White">
<ListBox Name="lstToolbox" Width="200" Height="280">
<ListBox.ItemTemplate> …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,您可以看到我正在尝试做什么,但它不起作用.我想要一个事件,我可以附加到我的用户控件之外,并在依赖项属性更改时触发.
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value"
, typeof(Double)
, typeof(ucSlider)
, new PropertyMetadata(50d, new PropertyChangedCallback(OnValueChanged)));
public Double Value
{
get { return (Double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public event PropertyChangedCallback OnValueChanged;
Run Code Online (Sandbox Code Playgroud) 例如:
public static class aVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName { public static void methodOne() }
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中只能写出类似的东西:
x = aVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
x.methodOne
Run Code Online (Sandbox Code Playgroud) 下面是一个Win32控制台应用程序过程,演示了各种指针对数组的依赖性.通过例如取消注释标记为"// uncomment ..."的行来更改原始数组(模型)中的值会导致输出发生更改.我的问题是如何在C#托管代码环境中获取或模仿这种行为(即不使用不安全和指针)?
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float model[100];
for(int i = 0; i < 100; i++) { model[i] = i; }
// uncomment these to alter the results
//model[5] = 5000;
//model[20] = 20000;
//model[38] = 38000;
static const int componentCount = 5;
float* coefs = model; // coefs points to model[0]
float* mean = coefs + componentCount; // mean points to model[0 + componentCount] == model[5]
float* cov = …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我只是将一个字符串(例如"medium")转换为其Enum值.我需要做的不是将Opacity作为固定的Enum类型,而是将其作为参数传递,以便该函数在任何枚举上运行.这似乎证明比我预期的更困难,即'Enum MyEnum'不起作用.有人解决吗?
public enum Opacity
{
Low,
Medium,
High
}
public static Enum StringToEnum(String str)
{
return (Opacity)Enum.Parse(typeof(Opacity), str, true); // Case insensitive
}
Run Code Online (Sandbox Code Playgroud) 我有一个从linq查询填充的匿名类型(grps).其中一个字段(DY)包含一个数组.
如果我运行此代码:
grps.ElementAt(0).Product = "kkk";
Run Code Online (Sandbox Code Playgroud)
我收到编译错误.
如果我运行此代码,我没有错误,但值不变.
grps.ElementAt(0).DYs[0] = 19;
Console.WriteLine(grps.ElementAt(0).DYs[0]); // not 19
Run Code Online (Sandbox Code Playgroud)
但是,如果我在grps上执行foreach,然后对每个数组执行嵌套步骤,我可以更改数组的值,并在嵌套循环中报告它们已更改.在嵌套循环之外,它们仍然保持不变.
我需要在匿名类型中更改数组中的值,但我无法弄清楚如何.
这让我感到恼火和困惑,因为我花了很多时间编写我认为工作正常的代码,但事实证明并没有产生任何错误.
UPDATE
对此的结果是,像往常一样,当我遇到问题时,这是因为我忘记在某事结束时坚持使用ToList().