为什么这个代码段的输出System.Int32代替Nullable<Int32>?
int? x = 5;
Console.WriteLine(x.GetType());
Run Code Online (Sandbox Code Playgroud) Substring().net System.String类中方法的定义是这样的
public string Substring(int startIndex)
Run Code Online (Sandbox Code Playgroud)
其中startIndex是"在这种情况下的子串的从零开始的起始字符位置"按照方法定义.如果我理解正确,这意味着它将给我一部分字符串,从给定的从零开始的索引开始.
现在,如果我有一个字符串"ABC"并使用不同索引的子字符串,我得到以下结果.
var str = "ABC";
var chars = str.ToArray(); //returns 3 char 'A', 'B', 'C' as expected
var sub2 = str.Substring(2); //[1] returns "C" as expected
var sub3 = str.Substring(3); //[2] returns "" ...!!! Why no exception??
var sub4 = str.Substring(4); //[3] throws ArgumentOutOfRangeException as expected
Run Code Online (Sandbox Code Playgroud)
为什么它不会为案例[2]抛出异常?
该字符串有3个字符,所以索引是[0, 1, 2],甚至ToArray(),ToCharArray()方法返回3个字符按预期!如果我尝试Substring()使用起始索引,它不应该抛出异常3吗?
我试图从我的角应用程序获取一个json字符串到Web API.在过去的6个小时里,我一直在寻找互联网,并且惨遭失败,弄清楚我做错了什么.
我检查了网络控制台,我可以看到json作为表单数据,但我的WEB api由于某种原因没有得到它.我看了几个其他帖子,但似乎没有任何帮助我的特定问题.任何方向都会很棒.我已经尝试使用"转换"修复,但这没有帮助.
进入WEB API的入口点
[HttpPost]
[Route("api/SkeltaInterfaceController/SaveWorkflow")]
public bool SaveWorkflow([FromBody] string json)
{
...
}
Run Code Online (Sandbox Code Playgroud)
角度呼叫
$scope.SaveWorkFlow = function () {
$http({
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
method: "POST",
url: webAPI,
data: {'DATA' : 'TEST DATA'
}
})
}
Run Code Online (Sandbox Code Playgroud)
编辑:我改变了角度调用
$scope.SaveWorkFlow = function () {
$http({
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
method: "POST",
url: webAPI,
data: {'DATA' : 'TEST DATA'}
})
}
Run Code Online (Sandbox Code Playgroud)
Web API看起来像这样
[HttpPost]
[Route("api/SkeltaInterfaceController/SaveWorkflow")]
public bool SaveWorkflow([FromBody] TestModel json)
{
...
}
Run Code Online (Sandbox Code Playgroud)
和模型
public class TestModel
{
public string DATA …Run Code Online (Sandbox Code Playgroud) 我正在浏览一些使用C#7新功能的代码,并使用ref locals和returns功能.
对于value-typesref本地变量获取引用(到实际存储)的位置,并且更新它更新原始项的值,似乎非常简单.
一些小的解释将有助于理解内存引用如何在ref本地化的情况下工作reference-types.我指的是下面代码的最后一行:
// A simple class
public class CoolClass
{
public string Name { get; set; }
public int Id { get; set; }
public CoolClass(string name, int id) => (Name, Id) = (name, id);
}
//Dummy method that returns first element with Id > 100
public CoolClass GetSpecialItem_Old(CoolClass[] arr)
{
for (int i = 0; i < arr.Length; i++)
if (arr[i].Id > 100)
{
return arr[i];
}
throw new Exception("Not found");
}
//Same …Run Code Online (Sandbox Code Playgroud) 我是我的应用程序的ASP.NET MVC v4,我正在使用Web优化功能(脚本和样式的捆绑和缩小).
现在,我理解的是(如果错误请纠正我),优化框架将在编译时查看包含的文件并进行配置.它将根据内容创建版本号(v = something).每次内容更改时,它都会重新创建版本哈希,客户端将获得更新的文件.
现在,有没有办法完成以下任务
[1]更新我服务器中js文件中的内容,并将更新后的内容提供给客户端,而无需重新构建和重新启动应用程序(我不是在这里更改捆绑配置,只是更新脚本中的文件内容)?
[2]更新脚本配置本身(例如,向捆绑包添加新脚本),并将其提供给客户端,而无需重新编译和重新启动应用程序?或者,至少没有重新编译?(我知道,通常我们在cs文件中定义包,但想知道是否有出路!)
[3]有没有办法使用我自己的版本号(比如来自配置文件,v = myCustomScriptVersion)而不是自动生成的版本哈希?
asp.net-mvc web-optimization asp.net-mvc-4 bundling-and-minification asp.net-bundling
如何在两个字符串字段上实现IComparable接口?
使用下面的Person类示例.如果将Person对象添加到列表中.如何根据Surname first THEN Forename对列表进行排序?
Class Person
{
public string Surname { get; set; }
public string Forname { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
就像是?:
myPersonList.Sort(delegate(Person p1, Person p2)
{
return p1.Surname.CompareTo(p2. Surname);
});
Run Code Online (Sandbox Code Playgroud) 我有一个具有非原始属性的类.
我需要更新父类的属性的一些子属性.
public class Parent
{
public string Abc { get; set; }
public Childclass Pos { get; set; }
}
public class Childclass
{
public string Value { get; set; }
}
List<Parent> parents = new List<Parent>()
Parent p1 = new Parent();
p1.Pos.Value = "1";
parents.Add(p1);
Parent p2 = new Parent();
p2.Pos.Value = "2";
parents.Add(p2);
Run Code Online (Sandbox Code Playgroud)
现在我需要更新Pos父母where Pos.Value == "2"?
我有一个MVC应用程序,其中包含几个简单的页面,这些页面主要在Web API调用上运行.为简单起见,我想将它们包含在同一个项目中.我可以启动并导航到我的页面,但是当我尝试通过Ajax调用我的API时,我一直收到404错误 - 它无法找到API函数.
这是我的javascript文件:
$(document).ready(function () {
//set the login button to call our test API function
document.getElementById("login_submit").addEventListener("click", GetUser);
});
function GetUser() {
var response = $.ajax({
url: '/api/User',
method: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("Success!");
},
error: function (request, status, error) {
alert(error);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
namespace MyProject.Controllers.API
{
public class UserController : ApiController
{
// GET api/<controller>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5 …Run Code Online (Sandbox Code Playgroud) 我有一个仅在生产环境中发生的非常奇怪的问题。异常有消息
“委托给实例方法不能有 null 'this'”。
抛出异常的方法非常简单,并且工作了很长时间,所以问题一定是环境中的模糊依赖,或者类似的东西......
我正在使用托管在 Azure 中的 ASP.NET Web API,控制器的操作方法是通过 AJAX 执行的。
这是抛出异常的代码:
public class BlacklistService : IBlacklistService
{
public bool Verify(string blacklist, string message)
{
if (string.IsNullOrEmpty(blacklist)) return true;
var split = blacklist.ToLower().Split(';'); // exception is thrown here
return !split.Any(message.Contains);
}
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪的相关部分:
at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
at System.MulticastDelegate.CtorClosed(Object target, IntPtr methodPtr)
at MyApp.Business.Services.BlacklistService.Verify(String blacklist, String message)
at MyApp.Business.Services.ContactMessageFactory.GetVerifiedStatus(String mensagem)
at MyApp.Business.Services.ContactMessageFactory.GetMailMessage(ContactForm contactForm)
at MyApp.Business.ContactEmailService.Send(ContactForm contactForm)
Run Code Online (Sandbox Code Playgroud)
有人可以找出导致此异常的可能原因吗?提前致谢。
我正在编写Windows窗体应用程序(c#),并且需要检测用户的计算机中是否安装了“ Microsoft-Edge”。
我当前正在使用此注册表位置:
[HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\PackageRepository\Packages\Microsoft.MicrosoftEdge_20.10240.16384.0_neutral__8wekyb3d8bbwe]
"Path"="C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
Run Code Online (Sandbox Code Playgroud)
用regex“ Microsoft.MicrosoftEdge”之后。如果“路径”存在,那么我知道边缘已安装。
有没有更好的方法来检测边缘?如果我检测到我正在Windows 10上运行,并且默认情况下Win10带有edge,那会更好吗?最好的方法是什么?