小编Ken*_*Kin的帖子

System.Object的BaseType应该与接口相同吗?

  • 摘要

    typeof(ISomeInterface).BaseTypenull; typeof(object).BaseType也是null根据定义.从而:

    typeof(object).BaseType==typeof(ISomeInterface).BaseType -> true 
    
    Run Code Online (Sandbox Code Playgroud)

    它(语义上)可能意味着System.Object一个接口,不是吗?在c#中,所有类都继承自System.Object,但接口不继承.接口实际上是契约,而不是任何类的基类型,并且只能从其他接口派生.

    所以我想知道是否typeof(object).BaseType应该与接口相同?

  • 说明

    我认为这是基本的数学逻辑.I处理null0typeof(object)1,使得找到的基类型某种类型的就像找到许多的因素.

    在这种假设下,null可以是任何类型的推导,例如0任意数量的倍数.并且typeof(object)将是任何类的基本类型,就像1是任何数字的因素,甚至本身.实际归国nulltypeof(object).BaseType,但是,打破了这一假设.这似乎说这0是一个因素,1但不是任何其他数字的因素.

    此外,如果我们使用一种方法来SomeType递归地找到基类型,我们不能总是说它SomeType不是一个类,因为它BaseTypenull,typeof(object).BaseType也是null.

    如果这似乎是矛盾的

    typeof(object).BaseType==typeof(object) -> true 
    
    Run Code Online (Sandbox Code Playgroud)

    提出它的基本类型本身,但实际上并不是实体和合同之间的区别?


更新:

我最初说过:

它可能意味着System.Object一个接口,不是吗?

我想说的是它似乎很困惑.这是我表达不好的描述的错,并且很抱歉导致答案集中在那上面.

.net c# reflection types

5
推荐指数
2
解决办法
1447
查看次数

如何检查字符串是否包含单个子串的出现?

我有这个:

string strings = "a b c d d e";
Run Code Online (Sandbox Code Playgroud)

我需要类似的东西string.Contains(),但我不仅需要知道字符串是否存在(如果在字母上方),而且还需要知道它是否仅存在一个单一时间.

我怎样才能做到这一点?

c# string

5
推荐指数
2
解决办法
5273
查看次数

构建组合矩阵

我确信这已被问了一百万次,但是当我搜索所有的例子都不太合适时,所以我想我还是应该问它.

我有两个数组,每个数组总共包含6个项目.例如:

string[] Colors=
    new string[] { "red", "orange", "yellow", "green", "blue", "purple" };

string[] Foods=
    new string[] { "fruit", "grain", "dairy", "meat", "sweet", "vegetable" };
Run Code Online (Sandbox Code Playgroud)

在这两个阵列之间,有36种可能的组合(例如"红色水果","红色颗粒").

现在我需要进一步将这些组合成六组唯一值.

例如:

meal[0]=
    new Pair[] { 
        new Pair { One="red", Two="fruit" }, 
        new Pair { One="orange", Two="grain" }, 
        new Pair { One="yellow", Two="dairy" }, 
        new Pair { One="green", Two="meat" }, 
        new Pair { One="blue", Two="sweet" }, 
        new Pair { One="purple", Two="vegetable" } 
    };
Run Code Online (Sandbox Code Playgroud)

在哪里吃饭

Pair[][] meal;
Run Code Online (Sandbox Code Playgroud)

在我的"餐"列表中不能重复任何元素.因此,只有一个"红色"项目和一个"肉"项目等.

我可以根据前两个数组轻松创建对,但我在如何最好地将它们组合成唯一组合方面做了一个空白.

c# algorithm combinations permutation matrix

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

检查字节是否为0x00

编写此方法的最具可读性(和惯用语)是什么?

private bool BytesAreValid(byte[] bytes) {
    var t = (bytes[0] | bytes[1] | bytes[2]);
    return t != 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要一个函数来测试一个不是以它开头的文件的前三个字节00 00 00.

没有做太多的字节操作.上面的代码对我来说似乎不正确,因为t推断了类型Int32.

c# byte bit-manipulation bytearray

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

如何合并返回匿名类型的lambda的LINQ查询?

我正在尝试将这两个列表组合在一起:

var quartEst = Quarterly_estimates
.OrderByDescending (q => q.Yyyy)
.ThenByDescending (q => q.Quarter)
.Where (q => 
    q.Ticker.Equals("IBM")
    &&
    q.Eps != null
    )
.Select (q => new {
    ticker = q.Ticker,
    Quarter = q.Quarter,
    Year = q.Yyyy,
    Eps = q.Eps})
.AsEnumerable()
.Where (q => Convert.ToInt32(string.Format("{0}{1}", q.Year, q.Quarter)) > Convert.ToInt32(finInfo) );

var quartAct = Quarterlies
.OrderByDescending (q => q.Yyyy)
.ThenByDescending (q => q.Quarter)
.Where (q => 
    q.Ticker.Equals("IBM")
    &&
    Convert.ToInt16(q.Yyyy) >= DateTime.Now.Year - 3
    )
.Select (q => new {
    Tick = q.Ticker, …
Run Code Online (Sandbox Code Playgroud)

c# linq union lambda

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

在运行时维护功能更改对象类型

长话短说

说我有以下代码:

// a class like this 
class FirstObject {
    public Object OneProperty {
        get;
        set;
    }

    // (other properties)

    public Object OneMethod() {
        // logic
    }
}

// and another class with properties and methods names 
// which are similar or exact the same if needed 
class SecondObject {
    public Object OneProperty {
        get;
        set;
    }

    // (other properties)

    public Object OneMethod(String canHaveParameters) {
        // logic
    }
}

// the consuming code would be something like this 
public static …
Run Code Online (Sandbox Code Playgroud)

c# reflection types runtime

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

代表的封装问题?

我想知道为什么这个有效?

例如,我有一些看起来像这样的执行器类:

public class Executor
{
    public void Execute(Action action)
    {
        action();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一些需要被执行的类看起来像:

public class NeedToBeExecuted
{
    public void Invoke()
    {
        Executor executor = new Executor();
        executor.Execute(DoSomething);
    }

    private void DoSomething()
    {
        // do stuff private
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么这是工作我将私有方法传递给其他类

这不是封装问题吗?

c# oop encapsulation

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

非对称加密和解密

我们假设我使用此站点上的算法使用公钥 - 私钥加密和解密数据:

CodeProject上C#.NET中的公钥RSA加密

现在,让我们说有人使用我的公钥使用另一种算法加密他的数据并将其发送给我.使用不同的算法(如网站上的算法),我能够使用我的私钥解密信息吗?或者这是不可能的,因为算法不同?

我的观点是,假设使用的密钥是正确的,如果使用不同的加密算法,最终结果是否总是相同的?

是否有一些标准的方法来加密信息,使其可以在不同的机器上解密,也许是不同的编程语言?

c# encryption cryptography pki

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

无数可数仍然是"可枚举的"?

像两个重叠的线段一样,我们可以找到无限的交点.列举所有这些点可能没有意义,我们可能只想表明这个集合是无限的.

浮点数已定义NegativeInfinityPositiveInfinity.表示计数序数的数字似乎不一定使用浮点数,但是,整数没有定义为表示无穷大的东西.

所以我试着实现无限的可枚举.但我突然对"可枚举"一词感到困惑.

有没有更好的方法来解决这个问题?并且无数可枚举仍然可以枚举

  • public partial class Infinity: IEnumerable<object> {
        IEnumerator<object> IEnumerable<object>.GetEnumerator() {
            for(; ; )
                yield return Infinity.Enumerable;
        }
    
        public IEnumerator GetEnumerator() {
            for(; ; )
                yield return Infinity.Enumerable;
        }
    
        public Infinity LongCount(
            Func<object, bool> predicate=default(Func<object, bool>)) {
            return Infinity.Enumerable;
        }
    
        public Infinity Count(
            Func<object, bool> predicate=default(Func<object, bool>)) {
            return Infinity.Enumerable;
        }
    
        public static readonly Infinity Enumerable=new Infinity();
    }
    
    Run Code Online (Sandbox Code Playgroud)

编辑:

谢谢回答.我混淆IEnumerableIEnumerator.GetEnumerator …

c# ienumerable terminology semantics

4
推荐指数
2
解决办法
1426
查看次数

对二进制索引树概念的理解较少

我在互联网上阅读了2 - 3个二进制索引树(AKA Fenwick树)的教程,但我不明白它实际上做了什么以及背后的想法是什么BIT.我读的教程是

请帮我让我明白BIT.

language-agnostic algorithm tree data-structures

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