小编Vim*_* CK的帖子

什么是新的.Net Native

今天我读了一篇关于MSDN上新的.Net Native的文章.

"使用.NET Native,Windows Store应用程序的启动速度提高了60%,并且内存占用空间更小.我们的第一个版本是开发人员预览版,允许您使用这个新编译器开发和测试应用程序.此.NET预览版提供你用C#的生产力来表现C++".

当然,这真的很有趣但是如果.Net Native是一个新的编译器,它提供了良好的性能,那么为什么我们需要去RyuJIT.什么是新的.Net Native?新的.Net Native和RyuJIT之间有什么区别,微软发布的编译器也称为Roslyn.那么Roslyn如何支持这个新的.Net Native.

.net c# .net-framework-version roslyn ryujit

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

不明确的构造函数调用错误

我有一个叫做接受的课Test,另一个接受.请参阅以下代码段.constructorAction<T>Func<T,T>

public class Test<T>
{
    //constructors
    public Test() { }
    public Test(Action<T> action) { }
    public Test(Func<T, T> action) { }

    //methods with same signature of constructor
    public void MyMethod1(Action<T> action) { }
    public void MyMethod2(Func<T, T> action) { }
}


public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Test<string> t1 = new Test<string>(this.MyMethod1);
        Test<string> t2 = new Test<string>(this.MyMethod2);

        Test<string> t = new Test<string>();
        t.MyMethod1(MyMethod1);
        t.MyMethod2(MyMethod2);
    }

    public void MyMethod1(string value) { …
Run Code Online (Sandbox Code Playgroud)

c# oop constructor

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

DynamicObject对空值的行为有所不同

这是一个DynamicDataObject派生自的类DynamicObject

 public class DynamicDataObject : DynamicObject
 {
        private readonly Dictionary<string, object> _dataDictionary = new Dictionary<string, object>();

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return _dataDictionary.TryGetValue(binder.Name, out result);
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (!_dataDictionary.ContainsKey(binder.Name))
            {
                _dataDictionary.Add(binder.Name, value);
                return true;
            }

            return false;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return _dataDictionary.Keys;
        }
  }
Run Code Online (Sandbox Code Playgroud)

我正在消费DynamicDataObject如下.

 public MainWindow()
 {
     InitializeComponent();
     dynamic person = new DynamicDataObject();
     person.FirstName = "Vimal";
     person.LastName = "Adams";
     person.Address …
Run Code Online (Sandbox Code Playgroud)

c# dynamic dynamicobject

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

为什么扩展方法表现不同?

派生类包含一个"计数"方法,它对类"派生"执行一些操作.另一方面,我有一个扩展方法,它也是"派生"类的目标.

Derived derived = new Derived();
derived.Count();
Run Code Online (Sandbox Code Playgroud)

通过调用上面的代码片段将在派生类中执行"Count"方法.为什么C#编译器在这种情况下不会警告并识别扩展方法.框架内部如何处理这个?

//Base class
public class Base
{
    public virtual string Count()
    {
        return string.Empty;
    }
}

//Derived class
public class Derived : Base
{
    public override string Count()
    {
        return base.Count();
    }
}

//Extension Methods for Derived class
public static class ExtensionMethods
{
    public static Derived Count(this Derived value)
    {
        return new Derived();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# extension-methods

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

接口行为在VB.Net中是不同的

接口在Vb.Net中的行为不同.下面是一个示例代码片段,其中IStudentinterface具有SayHello由类实现的方法Student.默认情况下,Access修饰符SayHello应为Public.通过将Access修饰符更改Private为不破坏现有代码,我仍然可以使用下面的代码访问此私有方法

Dim stdnt As IStudent = New Student
stdnt.SayHello()
Run Code Online (Sandbox Code Playgroud)

Access修饰符确定类中成员的范围,更多的私有成员只能从存在的类中访问.但是这里的Access Modifier理论,Encapsulation被打破了.

  • 为什么.net以这种方式设计?
  • Access修饰符和封装的概念真的被打破了吗?
  • .net框架如何在内部处理这种情况?

提前致谢

Module Module1

   Sub Main()
        Dim stdnt As IStudent = New Student
        stdnt.Name = "vimal"
        stdnt.SayHello()
   End Sub

End Module

Public Interface IStudent

   Property Name As String

   Sub SayHello()

End Interface

Public Class Student
   Implements IStudent

   Private Property Name As String Implements IStudent.Name

   Private Sub SayHello() Implements IStudent.SayHello
       Console.WriteLine("Say Hello!")
   End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

vb.net

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

为什么变量名作为下划线字符(“_”)不适用于解构

我已经将一个变量声明为下划线字符,_如下所示,编译器能够顺利执行代码。

 int _ = 10;
 _ += 10;

 onsole.WriteLine(_);
Run Code Online (Sandbox Code Playgroud)

但是,_对于如下所示的解构语法,编译器没有检测到命名为下划线字符的变量。

(string _, int age) = new Student("Vimal", "Heaven", 20);
Run Code Online (Sandbox Code Playgroud)

同时,编译器和 Visual Studio 智能感知_为下面显示的另一种语法检测名为下划线的变量。

var student  = new Student("Vimal", "Heaven", 20);
(string _, int age) details = student.GetDetails();

Console.WriteLine(details._);
Run Code Online (Sandbox Code Playgroud)

我知道没有人使用下划线字符来命名变量。为什么编译器在处理下划线_字符时不一致?

我不是discards在这里讨论 C# 。

Student示例中引用的类。

public class Student
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# .net-core .net-core-3.1

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

.Net 4.5中事件的标记扩展

WPF没有定义用于事件的标记扩展,第三方能够创建可以与事件一起使用的标记扩展.现在,WPF 4.5支持事件的标记扩展.任何人都可以通过优雅的例子帮助如何在.Net 4.5中实现这一目标吗?

wpf events markup-extensions .net-4.5 wpf-4.5

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

因子逻辑如何运作?

我有一个代码片段来找出给定值的阶乘数.我试图通过调试代码片段找出代码流.但我仍然对流程感到困惑.下面是我的代码片段,任何人都可以帮助我理解流程吗?

static void Main()
{
   long value = factorial(5);
}

static long factorial(long num)
{
   if (num <= 1)
        return 1;
   else
        return num * factorial(num - 1);
} 
Run Code Online (Sandbox Code Playgroud)

c# algorithm

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

如何检查变量在c#中保存实例或类型

我上课了 Test

public class Test { }
Run Code Online (Sandbox Code Playgroud)

现在我有一个object1包含实例的变量Test.

object object2 = new Test();

// some code

object2 = typeof(Test);
Run Code Online (Sandbox Code Playgroud)

object2Test在不同的场景中接受类的类型和实例.如何查看它的值.即实例Test或类型Test

c# windows-store-apps

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

无法在 WPF 中的 DataTemplate 中为 TextBox 设置前景

我正在尝试Foreground为 a 中TextBox指定的属性设置属性,DataTemplate但调用不起作用。

我有UserControl以下 XAML:

<UserControl x:Class="TextBoxColourTest.TextFrameControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             xmlns:clrtest="clr-namespace:TextBoxColourTest"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <DataTemplate x:Key="EditModeTemplate">
            <TextBox Text="Hello"></TextBox>
        </DataTemplate>

        <Style TargetType="{x:Type clrtest:TextFrameControl}">
            <Setter Property="ContentTemplate" Value="{StaticResource EditModeTemplate}"></Setter>
        </Style>

    </UserControl.Resources>

</UserControl>
Run Code Online (Sandbox Code Playgroud)

然后我有一些使用 TextFrameControl 的 XAML:

<Window x:Class="TextBoxColourTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:textBoxColourTest="clr-namespace:TextBoxColourTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <textBoxColourTest:TextFrameControl x:Name="TextFrameControl"></textBoxColourTest:TextFrameControl>
            <Button Content="Red" Click="OnMouseUpRed"></Button>
            <Button Content="Green" Click="OnMouseUpGreen"></Button>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

最后是我有按钮的事件处理程序来更改前景色的代码:

    namespace TextBoxColourTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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

对象引用行为

在下面的代码片段中,我有两个变量firstString,secondString它们保持相同的值"Hello".因此两个变量的引用位置是相同的.

var firstString = "Hello";
var secondString = "Hello";
bool isSameReference = Object.ReferenceEquals(firstString, secondString);
//same reference for both variables
Run Code Online (Sandbox Code Playgroud)

但更新secondString值为"嘿"不会更新firstString,即使它引用相同的位置.为什么这些变量没有得到更新,这引用了相同的参考位置?

secondString = "Hey..";
isSameReference = Object.ReferenceEquals(firstString, secondString); 
//reference changed but firstString not updated
Run Code Online (Sandbox Code Playgroud)

更新secondString为它的前一个值为"Hello"使引用相同.

secondString = "Hello";
isSameReference = Object.ReferenceEquals(firstString, secondString); 
//now the reference for both variables are same
Run Code Online (Sandbox Code Playgroud)

为什么c#有这种行为以及frmaework internaly如何处理这个?提前致谢

c# c#-4.0

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

通过父类加载基类数据

我有一个Fruit有很多变量mass, taste的类:...等我有一个类Apple,它添加了一些变量:size, texture...等等

我写了一个简单的函数来加载Fruit变量,并且不想复制所有它来填充Apple变量.

public void ReadFruitData(string Name, ref Fruit newFruit);

public void ReadAppleData(string Name, ref Apple newApple);

我想打电话ReadFruitDataReadAppleData,但不太清楚如何做到这一点,因为我无法通过newApplenewFruit

class Apple : Fruit

有什么想法我能做到吗?

c#

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

为什么 asp .net core 不在异步方法中记录异常?

我有一个 .asp 网络核心项目,它async在 Configure 方法中运行一些永无止境的任务。在注意到这些方法中的异常没有记录到控制台之后。我只是做了一个小测试。像这样的东西:

public async void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext context)

{
        try
        {
            Task.Run(async () => { throw new Exception("My exception"); });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
}
Run Code Online (Sandbox Code Playgroud)

为什么不在async方法中记录这些异常?如果我只是在配置中抛出异常,它会毫无问题地抛出并记录到控制台。

c# exception .net-core asp.net-core

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