Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

10
多种主要功能
我对此有些陌生,请耐心等待。我目前正在学习C#和Java,它们的相似之处之一是主要功能需要封装在一个类中。例如 public class HelloWorld { public static void main(String[] args) { // Some Code } } 现在我了解到主程序通常是运行程序时的“入口点”。因此,基本上,您的程序将在主要功能所在的地方开始执行。但是我相信在两种语言中,您都可以在多个类中具有多个主要功能。因此,当我编译具有多个主要功能的项目时,“入口点”在哪里?编译器如何知道从哪里开始?
69 c#  java  main 



5
样式和ControlTemplate之间的区别
您能否告诉我Style和ControlTemplate之间的主要区别是什么?什么时候或为什么要使用其中一个? 在我看来,它们是完全一样的。作为初学者,我认为我是错的,因此是我的问题。
69 c#  .net  wpf  xaml 

4
dotnet恢复警告NU1701
我在C#中使用.NET Core,当我这样做时dotnet restore,它出现以下错误: PS C:\ workspace \ Arbitrator> dotnet恢复 C:\ workspace \ Arbitrator \ Arbitrator.csproj:警告NU1701:使用'.NETFramework,Version = v4.6.1'代替了项目目标框架'.NETCoreApp,Version = v2.0'恢复了软件包'PusherClient 0.5.0'。这可能会导致兼容性问题。C:\ workspace \ Arbitrator \ Arbitrator.csproj:警告NU1701:使用'.NETFramework,Version = v4.6.1'代替了项目目标框架'.NETCoreApp,Version = v2.0'恢复了软件包'WebSocket4Net 0.14.1'。这可能会导致兼容性问题。 这个有问题的软件包是PusherClient。我只是按照NuGet文档进行导入。如何解决此警告?
69 c#  .net  nuget  .net-core 

4
将SQL转换为Linq左联接,并使用null
如何正确将此SQL转换为linq select t1.ProgramID from Program t1 LEFT JOIN ProgramLocation t2 ON t1.ProgramID = t2.ProgramID where t2.ProgramID IS NULL 我尝试了一下但是没有用 var progy = ( from u in db.ProgramLocations join b in db.Programs on u.ProgramID equals b.ProgramID into yG from y1 in yG.DefaultIfEmpty() where u.ProgramID == null where u.ProgramID == null select …
69 c#  .net  sql  linq 

7
从集合到列表的最快转换
我要避免的是: ManagementClass m = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection managementObjects = m.GetInstances(); List<ManagementObject> managementList = new List<ManagementObject>(); foreach(ManagementObject m in managementObjects){ managementList.Add(m); } 没有一种方法可以将该集合放入看起来像这样的List中: List<ManagementObject> managementList = new List<ManagementObjec>(collection_array);
69 c#  collections 

3
用于内部联接的实体框架查询
查询的内容是: select s.* from Service s inner join ServiceAssignment sa on sa.ServiceId = s.Id where sa.LocationId = 1 在实体框架中? 这是我写的: var serv = (from s in db.Services join sl in Location on s.id equals sl.id where sl.id = s.id select s).ToList(); 但这是错误的。有人可以引导我走这条路吗?


7
C#!条件属性?
C#有一个不 Conditional(!Conditional,NotConditional,Conditional(!))属性? 我知道C#具有一个Conditional属性: [Conditional("ShowDebugString")] public static void ShowDebugString(string s) { ... } 等于1等于: public static void ShowDebugString(string s) { #if ShowDebugString ... #endif } 但是在这种情况下,我想要相反的行为(您必须专门选择退出): public static void ShowDebugString(string s) { #if !RemoveSDS ... #endif } 这使我尝试: [!Conditional("RemoveSDS")] public static void ShowDebugString(string s) { ... } 不会编译。和: [Conditional("!RemoveSDS")] public static …

2
C#7.0是否适用于.NET 4.5?
我在Visual Studio 2017 RC中创建了一个项目,以检查是否可以在.NET Framework 4.5项目中使用新的C#7.0语言功能。在我看来,引用System.ValueTupleNuGet之后,新的元组运行良好。还有什么我应该考虑的,还是行得通的? 检查System.ValueTupleNuGet依赖项后,看起来好像不支持.NET Framework 4.0。是这种情况,还是有某种方法可以使新语言在此运行时中运行?
69 c#  .net-4.0  .net-4.5  c#-7.0 

6
使用运行时确定的类型实例化对象
我处于一种情况下,我想实例化一个将在运行时确定的类型的对象。我还需要执行对该类型的显式转换。 像这样: static void castTest(myEnum val) { //Call a native function that returns a pointer to a structure IntPtr = someNativeFunction(..params..); //determine the type of the structure based on the enum value Type structType = getTypeFromEnum(val); structType myStruct = (structType)Marshal.PtrToStructure(IntPtr, structType); } 这显然不是有效的代码,但我希望它传达了我正在尝试做的本质。我实际上正在使用的方法将必须在约35种不同类型上执行封送处理操作。我还有其他几种方法需要对同一组类型执行类似的操作。因此,我想将类型确定逻辑与这些方法隔离开,这样我只需要编写一次即可,并使这些方法保持清晰易读。 我必须承认自己是设计的新手。谁能建议一个解决这个问题的好方法?我怀疑可能有一个我不知道的合适的设计模式。

4
是什么使Enum.HasFlag如此缓慢?
我在进行一些速度测试时,发现Enum.HasFlag比使用按位运算慢大约16倍。 有谁知道Enum.HasFlag的内部原理,为什么它这么慢?我的意思是说慢两倍不会太糟糕,但是当它慢16倍时,它会使该功能不可用。 如果有人想知道,这是我用来测试其速度的代码。 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace app { public class Program { [Flags] public enum Test { Flag1 = 1, Flag2 = 2, Flag3 = 4, Flag4 = 8 } static int num = 0; static Random rand; static void Main(string[] args) { int …
69 c#  .net 

2
打开文件对话框,一个用于多个Excel扩展名的过滤器?
我想使用OpenFileDialog对象浏览到Excel文件。我想将过滤器设置为打开具有不同类型的excel扩展名的文件,例如:.xls,.xlsm,.xlsx等。 我正在使用的是这样的: OpenFileDialog of = new OpenFileDialog(); of.Filter = "Excel Files(.xls)|*.xls| Excel Files(.xlsx)|*.xlsx| Excel Files(*.xlsm)|*.xlsm"; 这可行,但是用户必须从OpenFileDialog的下拉列表中选择正确的excel文件类型。 有谁知道是否有一种方法可以对所有类型的Excel扩展应用一个过滤器? 类似于:“ ... Excel文件(.xls,.xlsx,.xlxm)| * .xls,*。xlsx,*。xlsm;” 预先感谢您的任何答复。

3
锁内锁
我想知道这种构造是否会导致错误: lock(sync) { // something lock(sync) { //something lock(sync) { //something } } } 我已经运行了这段代码,看起来还不错,但是在某些情况下可能会引发错误?
69 c#  locking 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.