小编Tos*_*shi的帖子

结果声明的说明

我知道,42是一切的答案,但这是怎么回事42

int x = -(~'+'|0xAB^1337); //42
Run Code Online (Sandbox Code Playgroud)

c#

23
推荐指数
3
解决办法
1231
查看次数

限制通用扩展方法扩展字符串

我有一个非常通用的扩展方法来显示控制台中的任何类型的列表:

public static void ShowList<T>(this IEnumerable<T> Values)
{
    foreach (T item in Values)
    {
        Console.WriteLine(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

不是我有一个string我可以使用这个方法

string text = "test";
text.ShowList();
Run Code Online (Sandbox Code Playgroud)

但是如果string它在我的应用程序中没有意义.

如何string从这种方法中排除?我读过一些关于

ShowList<T>(this IEnumerable<T> Values): Where != string //doesn't work
Run Code Online (Sandbox Code Playgroud)

c# methods extension-methods

18
推荐指数
2
解决办法
994
查看次数

为什么在对1进行异或时,否定值会改变结果?

我知道XOR的工作,

Console.WriteLine(1^1);  // returns 0
Run Code Online (Sandbox Code Playgroud)

结果

00000001
00000001 
--------
00000000 
Run Code Online (Sandbox Code Playgroud)

但是这又如何回归?

Console.WriteLine(-(-1^1)); // returns 2
Run Code Online (Sandbox Code Playgroud)

c# bit-manipulation xor bitwise-operators bitwise-xor

16
推荐指数
3
解决办法
1745
查看次数

ASP.Net 多行文本框最大长度

我一直在寻找一种方法来限制 TextBox 中的文本,看起来像这样

<asp:TextBox runat="server" ID="tbTest" TextMode="MultiLine"  
             MaxLength="20" Rows="5" Columns="30" >
</asp:TextBox>
Run Code Online (Sandbox Code Playgroud)

该物业 MaxLength="20"如果其他财产只能TextMode="MultiLine"设置。它甚至没有被渲染。

HTML 中的输出如下所示:

<textarea name="ctl00$ContentPlaceHolder1$tbTest" 
          id="ctl00_ContentPlaceHolder1_tbTest" rows="5" cols="30">
</textarea>
Run Code Online (Sandbox Code Playgroud)

如果您在 SO 上搜索解决此问题的解决方案,您会得到 2 种建议:

  • 添加 ( asp:RegularExpression) 验证器并验证控件
  • 添加一个 JavaScript,它会在达到限制时删除最后一个插入字符。

大多数答案是 2013 年以前的maxlength,所有浏览器的支持都是从 2013 年 IE10 发布开始的(参考)。

目前每个浏览器都支持这个属性 - 请自行测试:https : //jsfiddle.net/wuu5wne1/

如何强制 ASP.Net 将此属性应用于我的多行文本框?

html c# asp.net textbox webforms

7
推荐指数
2
解决办法
7432
查看次数

通过溢出从MinValue迭代到MaxValue

我有一个简单的循环,看起来应该是这样的

for (sbyte i = sbyte.MinValue; i <= sbyte.MaxValue; i++)
{
    Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是sbyte.MaxValue +1 = sbyte.MinValue,这永远不会满足结束条件.我的解决方法是使用intfrom -128,127但是还有原生sbyte方法吗?

c# for-loop

7
推荐指数
1
解决办法
150
查看次数

MVC将post参数传递给字典

我有MVC应用程序返回ResultObjekt后处理FormulaData对象.这是通过HTTP-Post调用的rest API

[HttpPost]
[ActionName("GetResult")]
public ResultObjekt GetResult([FromBody]FormularData values)
{

}
Run Code Online (Sandbox Code Playgroud)

问题:有没有办法从valuesa Dictionary<string, string>或a中读取所有属性IEnumerable<KeyValuePair<string, string>>

例如

public class FormularData
{
    public string Item1 { get; set; }
    public string Item2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

应该导致a Dictionary<string,string>()或a IEnumerable<KeyValuePair<string, string>>

有价值的 { {"Item1","Value1"}, {"Item2","Value2"}}

我以前的解决方案与之相关Querystring,HttpGet而不是HttpPost因为我改变了,Request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value)所以不再适用.


这是我目前的 - 不是那么漂亮的解决方案:

[HttpPost]
[ActionName("GetResult")]
public ResultObjekt GetResult([FromBody]FormularData values)
{
    List<KeyValuePair<string, string>> list = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc

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

在非ASP元素中使用代码服务器端脚本分隔符/ ASP.NET内联表达式

我收到此错误消息:

无法从其"可见"属性的字符串表示形式<%:false%>创建"System.Boolean"类型的对象.

当我尝试在我的ASP.net网站中运行此代码时:

<a runat="server" visible='<%: false %>' href="~/" >Home</a>
Run Code Online (Sandbox Code Playgroud)

有语法错误吗?false应该可以通过任何方法替换:

<asp:Panel runat="server" Visible='<%: GetTrueOrFalse() %>'>Home</a>
Run Code Online (Sandbox Code Playgroud)

asp.net

5
推荐指数
0
解决办法
334
查看次数

正确的过载选择

我有代码

//this runs 
string[] s_items = {"0","0"};
string s_output = string.Format("{0}{1}",s_items);

//this one throws a exception
int[] i_items = {0,0};          
string i_output = string.Format("{0}{1}",i_items);
Run Code Online (Sandbox Code Playgroud)

为什么第一个运行而第二个抛出异常?为何选择

int[]Format(String,?Object)过载

string[]Format(String,?Object[])过载

c#

5
推荐指数
1
解决办法
47
查看次数

string.Concat的11次重载的原因

我只是注意到该方法有11个重载 string.Concat()

public static string Concat(IEnumerable<string> values);
public static string Concat<T>(IEnumerable<T> values);
public static string Concat(object arg0);
public static string Concat(params object[] args);
public static string Concat(params string[] values);
public static string Concat(object arg0, object arg1);
public static string Concat(string str0, string str1);
public static string Concat(object arg0, object arg1, object arg2);
public static string Concat(string str0, string str1, string str2);
public static string Concat(object arg0, object arg1, object arg2, object arg3);
public static string Concat(string str0, string str1, string …
Run Code Online (Sandbox Code Playgroud)

c# string

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

Elasticsearch 等于 SQL %Like%

这里开始,我向自己询问此类查询的 elasticsearch 语法:

WHERE text LIKE "%quick%"
  AND text LIKE "%brown%"
  AND text LIKE "%fox%" 
Run Code Online (Sandbox Code Playgroud)

我的尝试(不幸的是没有成功)

  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "text": [
                    "*quick*",
                    "*brown*",
                    "*fox*"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)

sql search elasticsearch sql-like elasticsearch-5

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