小编Syl*_* B.的帖子

即使发生错误,C++也会删除数组

我是C++的新手,来自C#.这是代码:

void function(int n)
{
    double* array = new double[n];

   //some work that can return or throw an exception
   //...

   delete[] array;
   return;
}
Run Code Online (Sandbox Code Playgroud)

我知道usingC++中没有C#等价物.

有没有一种简单而优雅的方法来确保记忆将被释放?

c++ arrays memory-management

9
推荐指数
2
解决办法
539
查看次数

C#double.ToString()最大位数和尾随零

如何将a double转换为string6个最大位数并删除尾随零?

我希望有 :

2.123456123 -> "2.123456"
0.0000012 -> "0.000001"   (and not "1.2e-6")
12.45 -> "12.45"   (and not "12.450000")
36 -> "36"   (and not "36.000000")
Run Code Online (Sandbox Code Playgroud)

使用string.Format("{0:F6"}, value)输出尾随零并且string.Format("{0:G6"}, value)不适合第二个示例.

可以使用value.ToString("0.######)吗?

有没有相同的方法string.Format()呢?

c# string format

8
推荐指数
1
解决办法
4199
查看次数

知道 current() 是元素还是属性

在处理 XSLT 表时,我最终得到了一个带有一些current()xpath 的模板。我想知道它是指属性还是元素。

select="./@*"实际上,我无法使用模板过滤器或来做我想做的事情select="./*",因为我想一次性获取所有元素或属性,并在同一模板中根据类型应用不同的处理。

xml xslt xpath

4
推荐指数
1
解决办法
463
查看次数

c#在设计用于处理异常和保留堆栈跟踪的方法中重新抛出异常

我想编写一个处理异常的方法,并在catch块内调用.根据传递的异常的类型,异常要么作为新异常的内部异常传递,要么只是重新抛出.如何在第二种情况下保留堆栈跟踪?

示例:

public void TestMethod()
{
    try
    {
        // can throw an exception specific to the project or a .Net exception 
        SomeWorkMethod() 
    }
    catch(Exception ex)
    {
        HandleException(ex);
    }
}

private void HandleException(Exception ex)
{
    if(ex is SpecificException)
         throw ex; //will not preserve stack trace...
    else
         throw new SpecificException(ex);
}
Run Code Online (Sandbox Code Playgroud)

我不想做的是,因为模式在很多地方重复,并且没有因子分解:

try
{
    SomeWorkMethod();
}
catch(Exception ex)
{
    if(ex is SpecificException)
         throw;
    else
         throw new SpecificException(ex);
}
Run Code Online (Sandbox Code Playgroud)

c# exception-handling exception

3
推荐指数
1
解决办法
268
查看次数

从ListBoxItem获取ListBox对象

我正在处理一个DependencyProperty回调(PropertyChangedCallback),其中sender是一个ListBoxItem对象.我需要在代码中访问ListBox包含的代码ListBoxItem.

可能吗 ?

我试过listBoxItem.Parent但是确实如此null

.net c# wpf dependency-properties

1
推荐指数
1
解决办法
101
查看次数