Questions tagged «c#»

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

13
如何隐藏列(GridView)但仍访问其值?
我有一个带有DataSource(SQL数据库)的GridView 。我想隐藏一列,但是选择记录时仍然能够访问该值。有人可以告诉我该怎么做吗? 这是我要隐藏但仍要访问其值的列: <asp:BoundField DataField="Outlook_ID" HeaderText="OutlookID" /> 我尝试了所有隐藏列(property Visible="false")的方法,但是无法访问其值。
77 c#  asp.net  gridview  hide 


4
LINQ:从列表中选择对象不包含项的地方
我在这里用LINQ语法苦苦挣扎...我想在这里扔它。我在其他任何地方都找不到我要找的东西。 好,说我有这个: public class Bar { public int BarId { get; set; } } public class Foo { public List<Bar> BarList { get; set; } } List<Foo> fooBunch = GetABunchOfFoos(); //let's just assume I'm getting more than one List<Foo> fooSelect = new List<Foo>; List<Bar> filterBars = GetAFewBars(); //assume I'm getting …
77 c#  linq 

11
Richtextbox WPF绑定
到目前为止,Document在WPF中进行DataBinding的工作RichtextBox,我看到了2个解决方案,它们是从RichtextBox和派生的,并加上一个DependencyProperty带有“代理”的解决方案。 第一个或第二个都不令人满意。有人知道另一种解决方案,或者是可以进行数据绑定的商业RTF控件吗?正常TextBox不是替代方案,因为我们需要文本格式。 任何的想法?

2
ToLowerInvariant()有什么问题?
我有以下代码行: var connectionString = configItems. Find(item => item.Name.ToLowerInvariant() == "connectionstring"); VS 2010代码分析告诉我以下内容: 警告7 CA1308:Microsoft.Globalization:在方法...中,将对string.ToLowerInvariant()的调用替换为String.ToUpperInvariant()。 这是ToUpperInvariant()不是更可靠?
77 c#  .net  .net-4.0 

8
在方法内部使用const代替变量的优点
每当方法中有局部变量时,ReSharper建议将其转换为常量: // instead of this: var s = "some string"; var flags = BindingFlags.Public | BindingFlags.Instance; // ReSharper suggest to use this: const string s = "some string"; const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; 鉴于这些实际上是常量值(而不是变量),我知道ReSharper建议将其更改为const。 但是除此之外,使用const(例如,更好的性能)时是否还有其他优点可以证明使用合理的关键字const BindingFlags代替了方便和可读的var关键字? 顺便说一句:我刚刚在这里发现了一个类似的问题:Resharper总是建议我使用const string而不是string,但是我认为这更多地是关于类的字段,而我的问题是关于局部变量/ consts的。

3
使用.NET 4.0任务模式使用HTTPClient .ReadAsAsync将JSON反序列化为数组或列表
我正在尝试反序列化http://api.usa.gov/jobs/search.json?query=nursing+jobs使用.NET 4.0任务模式返回的JSON 。它将返回此JSON(“加载JSON数据” @ http://jsonviewer.stack.hu/)。 [ { "id": "usajobs:353400300", "position_title": "Nurse", "organization_name": "Indian Health Service", "rate_interval_code": "PA", "minimum": 42492, "maximum": 61171, "start_date": "2013-10-01", "end_date": "2014-09-30", "locations": [ "Gallup, NM" ], "url": "https://www.usajobs.gov/GetJob/ViewDetails/353400300" }, { "id": "usajobs:359509200", "position_title": "Nurse", "organization_name": "Indian Health Service", "rate_interval_code": "PA", "minimum": 42913, "maximum": 61775, "start_date": "2014-01-16", …

14
通过IP地址获取用户位置
我有一个用C#编写的ASP.NET网站。 在此站点上,我需要根据用户的位置自动显示起始页。 可以根据用户的IP地址获取用户所在城市的名称吗?
77 c#  asp.net 


8
根据条件从列表中删除项目
我有一个这样的结构: public struct stuff { public int ID; public int quan; } 并要删除产品所在的ID位置1。 我目前正在尝试这样做: prods.Remove(new stuff{ prodID = 1}); 而且不起作用。 谢谢大家

6
通过正则表达式验证文件类型
我有一个.NET Web窗体,该窗体具有一个与正则表达式验证器绑定的文件上传控件。该验证器需要验证仅允许上传某些文件类型(jpg,gif,doc,pdf) 当前执行此操作的正则表达式为: ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.doc|.DOC|.pdf|.PDF)$ 但是,这似乎不起作用...任何人都可以给我一点reg ex帮助吗?
77 c#  asp.net  .net  regex 

7
从ASP.NET MVC操作获取绝对URL
这可能是一个虚拟的问题,但我找不到明确的指示。我在MVC3 Web应用程序中有一个POCO类,其唯一目的是管理服务器中某些文件的备份。通常,它会创建一个备份并将文件名返回给控制器,该控制器会发送一封包含URL的电子邮件以进行下载。这可以正常工作,但是我无法建立要发送的绝对URL。无论使用哪种功能,我总是会得到一个相对URL,例如/Backup/TheFile.zip,而不是例如http://www.somesite.com/Backup/TheFile.zip。我试过了: VirtualPathUtility.ToAbsolute("~/Backup/SomeFile.zip"); HttpRuntime.AppDomainAppVirtualPath + "/Backup/SomeFile.zip"; Url.Content("~/Backup/SomeFile.zip"); 但它们都返回/Backup/SomeFile.zip之类的内容。任何的想法?

3
多行C#内插字符串文字
C#6通过以下语法为内插字符串文字带来了编译器支持: var person = new { Name = "Bob" }; string s = $"Hello, {person.Name}."; 这对于短字符串来说非常有用,但是如果要生成更长的字符串,必须在一行上指定它吗? 使用其他类型的字符串,您可以: var multi1 = string.Format(@"Height: {0} Width: {1} Background: {2}", height, width, background); 要么: var multi2 = string.Format( "Height: {1}{0}" + "Width: {2}{0}" + "Background: {3}", Environment.NewLine, height, width, background); 如果没有一行内容,我无法找到一种通过字符串插值实现此目标的方法: var multi3 = …

5
如何使用构造函数依赖项注入对asp.net核心应用程序进行单元测试
我有一个使用应用程序的startup.cs类中定义的依赖项注入的asp.net核心应用程序: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:FotballConnection:DefaultConnection"])); // Repositories services.AddScoped<IUserRepository, UserRepository>(); services.AddScoped<IUserRoleRepository, UserRoleRepository>(); services.AddScoped<IRoleRepository, RoleRepository>(); services.AddScoped<ILoggingRepository, LoggingRepository>(); // Services services.AddScoped<IMembershipService, MembershipService>(); services.AddScoped<IEncryptionService, EncryptionService>(); // new repos services.AddScoped<IMatchService, MatchService>(); services.AddScoped<IMatchRepository, MatchRepository>(); services.AddScoped<IMatchBetRepository, MatchBetRepository>(); services.AddScoped<ITeamRepository, TeamRepository>(); services.AddScoped<IFootballAPI, FootballAPIService>(); 这允许这样的事情: [Route("api/[controller]")] public class MatchController : AuthorizedController { private readonly IMatchService _matchService; …

6
是否可以为C#函数中的参数或可选参数提供默认值?
我可以在C#中提供默认参数吗? 在C中: void fun(int i = 1) { printf("%d", i); } 我们可以给参数一个默认值吗?在C#中可以吗?如果是这样,我们可以避免函数重载吗? 在现有函数中添加可选参数始终是一种不好的做法。如果您正在处理的项目必须引用具有函数的类,而我们使用可选值更改了参数,则可能会抛出运行时异常,即找不到该方法。 这是因为我们认为如果添加一个额外的可选值,则在许多地方使用该函数都无需更改代码。 function Add(int a, int b); 这将通过以下方式调用: Add(10, 10); 但是,如果我们添加这样的可选参数, function Add(int a, int b, int c = 0); 然后编译器期望 Add(10, 10, 0); 实际上,我们这样调用,Add(10, 10)而该函数在该类中不可用,并导致运行时异常。 这是在将新参数添加到函数中而被很多地方调用的同时发生的,我不确定每次都会发生这种情况。但我建议您重载该功能。 我们总是需要重载具有可选参数的方法。同样,如果您正在使用具有多个可选参数的函数,则最好使用参数名称传递值。 function Add(int a, int b, int c = 0); 使用以下方式调用此函数总是好的。 …
77 c# 

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.