小编now*_*ed.的帖子

函数返回一个const值

我有一个以下函数接受枚举值,并根据枚举值返回一个常量值(在不同的类中).现在我得到一个"Constant initializer missing"错误.

public const int Testfunction(TESTENUM TestEnum)
{
    switch(TestEnum)
    {
      case TestEnum.testval1:
         return testclass.constvalue;
      case TestEnum.testVal2:
         return testclass.constvalue1;
      case TestEnum.testVal3:
         return testclass.constvalue2;
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 函数的返回类型究竟如何?(我使用对象返回类型,这不会抛出任何错误)

  2. 有没有其他选择来实现相同的目标?

.net c# enums visual-studio-2010 .net-4.5

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

在c#中没有参数的方法仍然是一种方法?

我要做的就是检查列表B中的所有元素是否都在列表A中

        if (listA.All(element => listB.Contains(element))
        {
            return;
        }
Run Code Online (Sandbox Code Playgroud)

有人想出了另一个解决方案,说这会起作用并且有效!

       if (listA.All(listB.Contains))
            return;
Run Code Online (Sandbox Code Playgroud)

现在,(我知道它有效),

  1. 为什么编译器在第二种方法中不需要a()near contains?
  2. 如果在将来,比方说,我希望这比较不区分大小写,我将如何使用第二种方法?

提前致谢.

c# .net-4.0 visual-studio-2010 .net-4.5

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

我为什么要使用IDisposable而不是在c#中使用?

今天,我想用文件执行操作,所以我想出了这段代码

    class Test1
    {
        Test1()
        {
            using (var fileStream = new FileStream("c:\\test.txt", FileMode.Open))
            {
                //just use this filestream in a using Statement and release it after use. 
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但在代码审查中,我被要求实现IDisposable接口和Finalizer方法

    class Test : IDisposable
    {
        Test()
        {
            //using some un managed resources like files or database connections.
        }

        ~Test()
        {
            //since .NET garbage collector, does not call Dispose method, i call in Finalize method since .net garbage collector calls this
        }

        public void Dispose()
        {
            //release my files or …
Run Code Online (Sandbox Code Playgroud)

.net c# garbage-collection .net-4.5

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

c#中密封类的扩展方法

我有这个sealed班,

public sealed class A
{
    public string AName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

有人可以这样写一个扩展方法:

public static class Extensions
{
   public static void ExtensionMethodForA (this A a)
   {
      Console.WriteLine("A's Extension method!");
   }
}
Run Code Online (Sandbox Code Playgroud)

问题是,你如何防止这种情况?

.net c# extension-methods

4
推荐指数
2
解决办法
6625
查看次数

如何调用接受两个模型的Controller方法?

如果我们有一个带有如下方法的API控制器,这似乎是完全有效的:

[HttpPost] 
public IHttpActionResult DoStuff([FromBody]ModelA modelA, [FromBody]ModelB modelB)
Run Code Online (Sandbox Code Playgroud)

注意这两个[FromBody]属性.

问题是,如何调用这样的方法?

localhost/test/DoStuff/
Run Code Online (Sandbox Code Playgroud)

发布数据:

<?xml version="1.0"?>
<ModelA>
 ...
</ModelA>
<ModelB>
 ...
</ModelB>
Run Code Online (Sandbox Code Playgroud)

似乎没有得到承认.有什么想法吗?

编辑:错误数据如下:

<?xml version="1.0"?>
<Error>
  <Message>An error has occurred.</Message>
  <ExceptionMessage>Can't bind multiple parameters ('model1' and 'model2') to the request's content.
  </ExceptionMessage>
</Error>
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc asp.net-web-api

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

方法的最大参数数量是多少(WebApi?)

我在WebApi中有这个方法

[IHttpActionResult]
[Get]
public void GetSome(P1, P2 ....... Pn)
{
   ...///
}
Run Code Online (Sandbox Code Playgroud)

只是好奇:

任何想法,WebApi为此方法允许的最大参数数量是多少?

.net c# asp.net-web-api asp.net-web-api2

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

C#中的只读属性与只读成员变量

我有一个ExProperty类似下面的课程:

class ExProperty
{
    private int m_asimplevar;
    private readonly int _s=2;

    public ExProperty(int iTemp)
    {
        m_asimplevar = iTemp;  
    }

    public void Asimplemethod()
    {
        System.Console.WriteLine(m_asimplevar);
    }

    public int Property
    {
        get {return m_asimplevar ;}
        //since there is no set, this property is just made readonly.
    }
}

class Program
{
    static void Main(string[] args)
    {
        var ap = new ExProperty(2);
        Console.WriteLine(ap.Property);
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 将属性设置为只读或只写的唯一目的是什么?我看,通过下面的程序可以readonly达到相同的目的!

  2. 当我将该属性设置为只读时,我认为它应该不可写。当我使用

    public void Asimplemethod()
    {
        _s=3; //Compiler reports as "Read only field cannot …
    Run Code Online (Sandbox Code Playgroud)

c#

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

Xml 序列化 - 直接在根 - Xml - 元素下渲染对象列表

以下模型serializes

    [XmlRoot]
    public class A
    {
        [XmlAttribute]
        public string Period { get; set; }

        public List<C> B { get; set; }

    }

<?xml version=1.0>
<A Period="Today">
 <B>
    <C>
    </C>
    <C>
    </C>
  </B>
</A>
Run Code Online (Sandbox Code Playgroud)

现在,我不希望这样<B>(对象列表应该直接列在下面XmlRoot

生成的 XML 应该如下所示:

<A Period="Today">
  <C>
  </C>
  <C>
  </C>
</A>
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

.net c# xml-serialization c#-4.0

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

如果其中一个被更改,我是否应该将每个控制器方法更改为异步?

有这个控制器文件 SecretAuthController.cs

    public class SecretAuthController : Controller 
    {
          public ActionResult Auth()
          {
               //library method whose signature recently changed to async

              return ProcessTelemetry();
          }        
    }
Run Code Online (Sandbox Code Playgroud)

所以,我的同事将此代码更改为

        public class SecretAuthController : MyBaseController
        {
              public Async Task<ActionResult> Auth()
              {
                   //library method whose signature recently changed to async                  
                   await MyLib.GetTaskDetails(Id);
                   return ProcessTelemetry();
              }        
        }
Run Code Online (Sandbox Code Playgroud)

在此过程中,他们还将所有其他控制器方法签名也更改为使用 1 async

所以,有些方法看起来像

     public Async Task<ActionResult> ExFooTest()
     {
           //There is no awaitable call here
           //normally returns.
     }
Run Code Online (Sandbox Code Playgroud)

对此有几点疑问:

  1. 如果没有 awaitable 调用,更改方法签名以使用async是否明智?
  2. 如果我们想更改一个操作方法签名以使用async,是否需要更改所有操作方法签名,这是一个通用规则吗?(假设他们不等待

.net c# asp.net-mvc asynchronous async-await

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

在 ASP.NET 中设置 SameSite=None 和 Secure

阅读有关SameSite为防止跨站点伪造而强制执行的更改。来源:https : //blog.chromium.org/2019/10/developers-get-ready-for-new.html

我正在尝试将其值设置为“无”并Secure按照宣传的方式使用。

我目前的web.config设置如下:

<system.web>
    <sessionState cookieless="UseCookies" 
       timeout="20" 
       cookieSameSite="None" 
       xdt:Transform="Replace" 
       xdt:Locator="Match(cookieless)"/>
  </system.web>
Run Code Online (Sandbox Code Playgroud)

文档来源:https ://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.sessionstatesection.cookiesamesite ? view = netframework-4.8#System_Web_Configuration_SessionStateSection_CookieSameSite

但我仍然收到以下错误:

A cookie associated with a resource at `mywebsite.net` was set with `SameSite=None` but without `Secure`. A future release of Chrome will only deliver cookies marked `SameSite=None` if they are also marked `Secure`.
Run Code Online (Sandbox Code Playgroud)

如何secure在上述 web.config 文件中指定属性?任何线索将不胜感激。

c# asp.net cookies web-config samesite

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