小编Ehs*_*jad的帖子

为什么比较两个字符串作为对象导致意外结果

考虑以下代码.

object str = new string(new char[] { 't', 'e', 's', 't' });
object str1 = new string(new char[] { 't', 'e', 's', 't' });
Console.WriteLine(str==str1); // false
Console.WriteLine(str.Equals(str1));  // true
Run Code Online (Sandbox Code Playgroud)

我理解在这里工作的相等运算符,因为我们已经隐式地转换为对象,等于运算符检查两者的引用是否相等并返回false.

但我对第二个问题感到困惑,返回true看起来它正在调用String类型提供的Equals覆盖实现,并且如果它们相等则检查字符串的内容.

我的问题是它为什么不检查运算符的内容相等性,它们的实际类型是字符串而不是对象.对 ?

以下代码输出两者:

object str = "test";
object str1 = "test";
Console.WriteLine(str==str1); // true
Console.WriteLine(str.Equals(str1)); // true
Run Code Online (Sandbox Code Playgroud)

c# inheritance equality equals

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

C# - 添加实现接口到字典的对象

我有一本字典:

private Dictionary<Type, IExample> examples;
Run Code Online (Sandbox Code Playgroud)

我有两个实现接口的类:

public class Example1 : IExample
{
}

public class Example2 : IExample
{
}
Run Code Online (Sandbox Code Playgroud)

我已经创建了一种从字典中获取实例的方法,如果它存在但是我试图找出一种方法来实例化一个新对象(如果它不存在).

public T GetExample<T>() where T : IExample
{
    // Return the signal if it exists
    if (examples.ContainsKey(typeof(T)))
    {
        IExample value;

        if (!examples.TryGetValue(typeof(T), out value))
        {
            // unable to get value
        }

        return (T)value;
    }

    // Stuck on this line here. How exactly do I instantiate a new example if it doesn't exist.
    examples.Add(typeof(T), new );

    return default(T);
}
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

c# generics dictionary

14
推荐指数
2
解决办法
1946
查看次数

C#构造函数extern修饰符的用途

C#构造函数extern修饰符的用途是什么?

我知道使用extern METHODS来调用Win32函数,但是CONSTRUCTORS呢?

请举出实际例子.

请注意:

class MyClass
{
    public extern MyClass();
}
Run Code Online (Sandbox Code Playgroud)

.net c#

13
推荐指数
2
解决办法
1818
查看次数

Linq Lambda与查询语法性能

我今天在我的项目中看到了一个linq查询语法,它以List这种方式从特定条件的项目开始计算:

int temp =  (from A in pTasks 
             where A.StatusID == (int)BusinessRule.TaskStatus.Pending     
             select A).ToList().Count();
Run Code Online (Sandbox Code Playgroud)

我想通过写它来重构它,就像使用Count()更易读,我认为它会表现得更好,所以我写道:

int UnassignedCount = pTasks.Count(x => x.StatusID == (int)BusinessRule.TaskStatus.Pending);
Run Code Online (Sandbox Code Playgroud)

但是,当我通过将StopWatchlambda表达式使用的时间进行检查时,总是比查询synax更多:

Stopwatch s = new Stopwatch();
s.Start();
int UnassignedCount = pTasks.Count(x => x.StatusID == (int)BusinessRule.TaskStatus.Pending);
s.Stop();
Stopwatch s2 = new Stopwatch();
s2.Start();
int temp =  (from A in pTasks 
             where A.StatusID == (int)BusinessRule.TaskStatus.Pending
             select A).ToList().Count();
s2.Stop();
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么会这样吗?

c# linq performance lambda linq-to-objects

13
推荐指数
2
解决办法
5647
查看次数

Asp.net页面生命周期和Asp.net Mvc页面生命周期有什么区别?

Asp.net页面生命周期和Asp.net Mvc页面生命周期有什么区别?

Asp.net页面生命周期只需记住SILVER U
s-启动
I-Initialization
L-Load
V-验证
电子事件处理
R
-Rendering U -Unload
Mvc和Asp.net页面的实际区别是什么?

asp.net asp.net-mvc

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

单击时WPF更改按钮背景图像

我创建了一个Button并设置了它的背景Image.我想要的是当Button点击时,我想Image用另一个替换背景

我怎么能做到这一点?

这是我的代码Button:

<Button x:Name="PopulationReporting" Click="PopulationReporting_Clicked" Width="172" 
       Height="60" Margin="57,170,57,184">
    <Button.Background >
        <ImageBrush ImageSource="images/img-2.png"  />
    </Button.Background>
</Button>
Run Code Online (Sandbox Code Playgroud)

wpf image click button

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

如何将字符串列表转换为guid列表

我有以下代码行创建一个字符串列表.

List<string> tstIdss = model.Ids.Where(x => x.Contains(entityId)).Select(x => x.Split('_').First()).ToList();
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为Guids列表.即List<Guid> PermissionIds.

model.PermissionIds= Array.ConvertAll(tstIdss , x => Guid.Parse(x));
Run Code Online (Sandbox Code Playgroud)

我尝试了上述方法,但得到以下错误.model.PermissionIds在我的模型类中实现如下.

public List<Guid> PermissionIds { get; set; }
Run Code Online (Sandbox Code Playgroud)

错误3

无法从用法中推断出方法'System.Array.ConvertAll(TInput [],System.Converter)'的类型参数.尝试显式指定类型参数.

c#

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

未绑定的类路径容器:项目中的'JRE系统库[Java SE 6 [1.6.0_65-b14-462]]'

我正在尝试在Eclipse中运行现有的Java项目,而且我是Java和Eclipse的新手,所以我无法弄清楚为什么这个错误会出现在项目中.

这是完整的错误:

描述资源路径位置类型未绑定的类路径容器:项目"INFO 2413服务器"中的"JRE系统库[Java SE 6 [1.6.0_65-b14-462]]"信息2413服务器构建路径构建路径问题

java eclipse jvm

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

在按钮单击上渲染部分视图

所以我已经尝试了一切,但我根本无法返回一个小的局部视图并将其放在div元素中.这是我的父视图

<button id="doctors">Doktori</button>
<button id="apointments"> Pregledi</button>

 <div id="someDiv">
 </div>

<script>
    document.getElementById("doctors").onclick = function () { myFunction() };
    function myFunction() {

        $("#someDiv").load("@Html.Raw(Url.Action("doctorsview", "HomeController")) ")
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我在.load()函数之前放了一些警告("message"),它显示了一条消息,但是我把它放在.load()之后,它没有显示它.我曾尝试使用和不使用html.raw().这是我的控制器动作

public ActionResult doctorsview()
{
    return PartialView("~/Views/_Doktor.cshtml", new Doktor());        
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

asp.net-mvc partial-views asp.net-mvc-4

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

类的in修饰符有什么意义

C#7.2引入了in参数的修饰符,这对于结构特别是对于只读结构非常有意义.

它也可以用于参考类型

void Method(in StringBuilder value) { }
Run Code Online (Sandbox Code Playgroud)

默认情况下,引用类型是通过引用传递的,in上面的示例中只是一个冗余修饰符吗?

value = null你使用时是禁止的in,这是否意味着它只是通过将原始引用传递给堆位置并阻止更改来保留引用地址的副本?

c# c#-7.2

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