小编Noe*_*mer的帖子

如何检测到StackOverflowException?

TL; TR
当我问这个问题时,我假设a StackOverflowException是一种阻止应用程序无限运行的机制.这不是真的.
A StackOverflowException未被检测到.
当堆栈没有分配更多内存的容量时抛出它.

[原始问题:]

这是一个普遍的问题,每个编程语言可能有不同的答案.
我不确定C#以外的语言如何处理堆栈溢出.

我今天经历了例外,并一直在思考如何StackOverflowException检测到它.我相信不可能说fe是否深度为1000次调用,然后抛出异常.因为在某些情况下,正确的逻辑可能会那么深.

在我的程序中检测无限循环的逻辑是什么?

StackOverflowExceptionclass:
https://msdn.microsoft.com/de-de/library/system.stackoverflowexception%28v=vs.110%29.aspx类文档中

提到的交叉引用StackOverflowException:https:
//msdn.microsoft.com/de -de /库/ system.reflection.emit.opcodes.localloc(v = vs.110)的.aspx

我刚刚在stack-overflow这个问题上添加了标记,并且描述说当调用堆栈消耗太多内存时它会被抛出.这是否意味着调用堆栈是我的程序的当前执行位置的某种路径,如果它不能存储更多的路径信息,那么抛出异常?

c# stack-overflow infinite-loop

67
推荐指数
5
解决办法
8282
查看次数

为什么从<T>到<U>的隐式转换运算符接受<T?>?

这是一种我无法理解的怪异行为.在我的例子我有一个类Sample<T>和隐式转换操作符TSample<T>.

private class Sample<T>
{
   public readonly T Value;

   public Sample(T value)
   {
      Value = value;
   }

   public static implicit operator Sample<T>(T value) => new Sample<T>(value);
}
Run Code Online (Sandbox Code Playgroud)

使用空值类型为出现问题时,T例如int?.

{
   int? a = 3;
   Sample<int> sampleA = a;
}
Run Code Online (Sandbox Code Playgroud)

这里是关键部分:
在我看来,这不应该编译,因为Sample<int>定义了一个转换,从intSample<int>而不是从int?Sample<int>.但它编译并成功运行!(我的意思是调用转换运算符3并将其分配给该readonly字段.)

它变得更糟.这里不调用转换运算符,sampleB将其设置为null:

{
   int? b = null;
   Sample<int> sampleB = b; …
Run Code Online (Sandbox Code Playgroud)

c# generics nullable value-type implicit-conversion

52
推荐指数
3
解决办法
3163
查看次数

在C#中从派生类调用私有基方法

派生类如何从基类调用方法.
另一方面,其他课程不应该有访问权限.

我的情况:
我有一个基类,我写了一个私有方法来注册一些值.

private void register(string param1, int param2){//...}
Run Code Online (Sandbox Code Playgroud)

我这样做是为了允许子类注册不同的东西.
问题是,派生类无法访问基类的私有方法或字段.
这对我来说很有意义,因为私有意味着私人.

我不想将该方法设为公共,因为其他类不应该能够调用此方法.
有人可以提供解决方案或指导我更好的设计吗?

c# access-modifiers

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

如何在Rust中检查字符是否是Unicode换行符(不仅是ASCII)?

每种编程语言都有自己的解释\n\r.Unicode支持可以表示新行的多个字符.

Rust参考:

空白转义是字符U + 006E(n),U + 0072(r)或U + 0074(t)之一,表示Unicode值U + 000A(LF),U + 000D(CR)或U + 0009(HT).

根据该声明,我会说Rust字符是一个新行字符,如果它是\n或者\r.在Windows中,它可能是组合\r\n.我不确定.

以下怎么样?

  • 下一行字符(U + 0085)
  • 行分隔符(U + 2028)
  • 段落分隔符(U + 2029)

在我看来,我们错过了像char.is_new_line().我查看了Unicode字符类别,但找不到新行的定义.

我是否必须提出我自己对Unicode换行符的定义?

unicode newline carriage-return linefeed rust

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

比较两个类型为T的System.Enum

我现在非常接近了解Generics(我认为).
但是,只是认为System.Enum不容易实现为泛型类型.我有这门课:

public class Button<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {
   public TEnum Identifier {
        get;
        private set; //Set in the ctor
    }
}
Run Code Online (Sandbox Code Playgroud)

public abstract class AbstractInputDevice<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {

   private List<Button<TEnum>> _buttons = new List<Button<TEnum>>();

   public Button<TEnum> GetButton(TEnum Identifier){
        foreach(Button<TEnum> button in _buttons){
            if(button.Identifier == Identifier) //<- compiler throws
                return button;
        }
        Debug.Log("'" + GetType().Name + "' cannot return an <b>unregistered</b> '" + typeof(Button<TEnum>).Name + "' that listens to '" …
Run Code Online (Sandbox Code Playgroud)

c# generics comparison enums

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

大的,每像素1位的位图导致OutOfMemoryException

我想做的是从磁盘加载图像并从中创建一个BitmapSource.
图像为14043px x 9933px,为b/w(1bpp).但我碰到了一个OutOfMemoryException因为以下代码消耗大约800 MB RAM.

以下代码生成ImageSource特定文件的维度.
我这样做是为了看看我是否可以在不使用磁盘上的实际文件的情况下使其工作.

public System.Windows.Media.ImageSource getImageSource(){

    int width = 14043;
    int height = 9933;

    List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
    colors.Add(System.Windows.Media.Colors.Black);
    colors.Add(System.Windows.Media.Colors.White);

    BitmapPalette palette = new BitmapPalette(colors);
    System.Windows.Media.PixelFormat pf = System.Windows.Media.PixelFormats.Indexed1;

    int stride = width / pf.BitsPerPixel;

    byte[] pixels = new byte[height * stride];

    for (int i = 0; i < height * stride; ++i)
    {
         if (i < height * stride / 2)
         {
               pixels[i] = 0x00;
         }
         else …
Run Code Online (Sandbox Code Playgroud)

c# wpf bitmap out-of-memory

7
推荐指数
2
解决办法
1765
查看次数

C++ Union用法

到目前为止,我刚刚使用了工会来存储成员A或成员B.
我现在发现自己想要在运行时更改已使用的成员.

union NextGen {
    std::shared_ptr<TreeRecord> Child = nullptr;
    std::vector<std::shared_ptr<TreeRecord>> Children;
};
Run Code Online (Sandbox Code Playgroud)

我目前的用法:

void TreeRecord::AddChild(const std::shared_ptr<TreeRecord>& NewChild) {
    if(_childCount == 0) {
        _nextGeneration.Child = NewChild;
        _childCount++;
    } else if(_childCount == 1) {
        //This is not clear to me:
        //Do I have to set Child to nullptr first?
        //Do I need to clear the Children vecor?
        //Or will it work like this?
        _nextGeneration.Children.push_back(_nextGeneration.Child);
        _nextGeneration.Children.push_back(NewChild);
        _childCount++;
    } else {
        _nextGeneration.Children.push_back(NewChild);
        _childCount++;
    }
}
Run Code Online (Sandbox Code Playgroud)

新实施(尝试):

typedef std::shared_ptr<TreeRecord> singlechild_type;
typedef std::vector<std::shared_ptr<TreeRecord>> children_type; …
Run Code Online (Sandbox Code Playgroud)

c++ unions

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

是否有类似可以实现的类?

我想编写一个继承自A(base)的
类X(this)可以执行B(?)的方法,并且必须实现C(接口)的成员.


实现A和C不是问题.但是由于X不能从多个类派生,所以X似乎不可能继承A和B的逻辑.注意A是非常重要的基类,B几乎是一个接口但包含可执行行为.我不希望B成为接口的原因是因为每个继承或实现它的类的行为都是相同的.

我是否真的必须将B声明为接口并为每个需要B行为的X实现完全相同的10行代码?


2个月后,
我正在学习C++,以便在UE4(虚幻引擎4)中使用它.
由于C++比C#严格得多,它实际上包含一个描述这种行为的模式 实现 idom术语:这些被称为mixin.

你可以阅读一个关于C++混入段落这里第9页(第二款).

c# inheritance interface multiple-inheritance abstract

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

是否在编译时对C#中的文字进行了算术运算?

很短的问题,但我现在无法在网上找到解决方案.

int test = 1 + 2;
Run Code Online (Sandbox Code Playgroud)

1 + 2在运行或编译时执行?

问的理由:我认为大多数人有时会使用文字而没有说明为什么使用它或它意味着什么,因为他们不想通过运行计算浪费一点性能,我相信计算在编译期间发生并且对性能:

int nbr = 31536000; //What the heck is that?
Run Code Online (Sandbox Code Playgroud)

代替

int nbr = 365 * 24 * 60 * 60; //I guess you know what nbr is supposed to be now...
Run Code Online (Sandbox Code Playgroud)

c# performance literals

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

应该装箱型时是否有最佳做法?

在C#中,有结构和类.结构通常(即有异常)堆栈分配,类总是堆分配.因此,类实例会对GC施加压力,并且被认为比结构"慢".Microsoft有一个最佳实践指南何时在类上使用结构.这表示在以下情况下考虑结构:

  • 它在逻辑上表示单个值,类似于原始类型(int,double等).
  • 它的实例大小小于16个字节.
  • 这是不可改变的.
  • 它不必经常装箱.

在C#中,通常认为使用大于16字节的结构实例比垃圾收集类实例(动态分配)更糟糕.

在速度方面,盒装实例(堆分配)何时比非盒装等效实例(堆栈分配)更好?关于何时应该动态分配(在堆上)而不是坚持默认的堆栈分配,是否有任何最佳实践?

heap stack boxing memcpy rust

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