小编now*_*ed.的帖子

C# - 类,构造函数和Resharper - 澄清

我在visual studio中使用Resharper工具

考虑下面写的一个非常简单的类.

class Test()
{

   //constructors do not have any return type.   
   Test()
   {
      System.Console.WriteLine("Hello World!");
   } 

   static Test()
   {
        System.Console.WriteLine("Hello World in Static constructors");
   }

   public void A()
   {
      System.Console.WriteLine("A simple function in a class");
   }

}


class Program
  {
    static void Main(string[] args)
    {
        var asa = new Test(); //Implicitly Typed local variables.
        asa.A();

    }
}
Run Code Online (Sandbox Code Playgroud)

使用var(编译器必须从初始化语句右侧的表达式推断变量的类型).

我有一些澄清问题,他们在下面.

  1. 编译器的额外负担?
  2. 一个班级可以拥有多少个构造函数?
  3. 为什么静态构造函数首先调用?(我通过断点检查了吗?)
  4. 为什么不测试asa = new Test(); Resharper不是首选的?
  5. 首先使用Resharper作为初学者真的是个好主意吗?(我自己是C和.net编程的新手!)

提前致谢.

.net c# resharper visual-studio-2010

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

C#中的工厂模式

我有点能够理解工厂模式并想出了这个实现。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the fruit name to get the Fruit!");
        string fruit = Console.ReadLine();

        FruitsList fruits;
        if (Enum.TryParse(fruit, true, out fruits))
        {
            var fruitWeight = GetFruitWeight(fruits);
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Fruit Name is undefined!");
            Console.ReadLine();
        }
    }

    private static object GetFruitWeight(FruitsList fruitNumber)
    {
        switch (fruitNumber)
        {
            case FruitsList.Banana:
                return new Banana();
            case FruitsList.Apple:
                return new Apple();
        }

        return null;
    }
}

internal class Banana : IFruits
{
    public float ReturnFruitWeight()
    {
        return (float)10.00; …
Run Code Online (Sandbox Code Playgroud)

.net c# design-patterns .net-4.5

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

c#中属性的单例模式

我正在尝试为我的CsvConfiguration属性调整单例策略.

如果配置已可用,则只需返回配置.否则,获取配置并返回相同,我能够构建此代码.

    public Rootpdf pdfConfiguration
    {
        get
        {
            Rootpdf pdfConfiguration = null;
            try
            {
                if (pdfConfiguration == null)
                {
                    //retrieve the configuration file.
                    //load the configuration and return it!
                }
                else
                {
                    return pdfConfiguration;
                }
            }
            catch (Exception e)
            {
                Log.Error("An error occurred while reading the configuration file.", e);
            }

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

优点(我希望):每当我的pdfConfiguration需要时,如果它已经可用,我可以返回它.无需每次都加载配置文件并计算配置.

我的查询:resharper!resharper告诉代码

   if (pdfConfiguration == null) //The expression is always true.
Run Code Online (Sandbox Code Playgroud)

它是否真的是resharper的一个问题,它不明白我遵循这个单例模式?

要么

我根本不遵循单身人士模式吗?

c# design-patterns .net-4.0 .net-4.5

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

与命名空间System.Collections一起使用时,IEnumerable报告编译器错误

我正在看这个Squares已经存在于Internet中的扩展方法.我无法得到这个编译.编译器报告类似"非泛型类型`System.Collections.IEnumerable'不能与类型参数一起使用".

任何想法下面的代码有什么问题?

任何帮助深表感谢.

using System.IO;
using System;
using System.Collections;

static class Program {

     static IEnumerable<int> Squares (this int from, int to) {
        for (int i=from;i<=to;i++)
        {
            yield return (int)i*i;
        }
    }

    static void Main(string[] args)
    {
        var min=1;
        foreach (int i in min.Squares(4))
        {
            Console.WriteLine(i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# extension-methods .net-4.0

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

从MVC 4中的URL获取"Id"值

我的网址是这样的,

localhost:19876/PatientVisitDetail/Create?PatientId=1

我必须PatientId从URL中检索并将其传递给请求.

我试过了,

    Url.RequestContext.Values["PatientId"] => System.Web.Routing.RequestContext does not contain a definition for 'Values' and
no extension method 'Values' accepting a first argument of type 'System.Web.Routing.RequestContext'
Run Code Online (Sandbox Code Playgroud)

我再试一次,

RouteData.Values["PatientId"]  => an object reference is required for the non static field, method
or property 'System.Web.Routing.RouteData.Values.get'
Run Code Online (Sandbox Code Playgroud)

编辑:

根据杰森在下面的评论,我试过Request["SomeParameter"]并且它有效.但是,还有一个警告要避免这种情况.

任何想法如何避免我的情况?

我的情景:

Create我的控制器中有一种用于创建新患者的动作方法.

但是,我需要回到最后一页,

如果我给出类似的东西,

 @Html.ActionLink("Back to List", "Index") 


=> this wont work because my controller action method has the following signature,

public ActionResult Index(int patientId = 0)
Run Code Online (Sandbox Code Playgroud)

所以,我必须patientId …

c# url routing .net-4.0 asp.net-mvc-4

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

字符串连接如何在C#中进行,用于var q ="A"+"B"+"C"

它更为人所知strings are immutable=>意思,那the contents of the object cannot be changed after it is created.

所以,面试问题是:

在此声明中创建了多少个对象?

string q = "A" + "B" + "C";

我回答two是因为"A"+"B"创建了一个对象并且与"C"连接创建了另一个对象.

但是,她说its wrong.有什么想法吗?

如果在这种情况下发生多个字符串的连接会发生什么?

.net c# string immutability visual-studio-2010

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

如何在UML中将Business层映射到Model对象?

刚开始Star UML绘制一个class diagram.

与任何应用程序一样,MyBLL(业务层)Model在与DAO(数据库层)交互之后创建一个实例.

或多或少,简化(为清晰起见)类图如下所示:

在此输入图像描述

很明显,BLL完成后将不再使用模型类.

  1. 我应该在这里使用什么?aggregationcomposition映射MyBLLMyModel.

  2. 我使用了一个composition从关系MyBLLMyDAO.我的逻辑是在BLL图层存在之后,MyDAO将不再存在,因为它被引用MyBLL.它是否正确?

注意:这是WebAPI我使用C#创建的项目.

.net c# uml staruml asp.net-web-api

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

将行(如果尚不存在)插入到已存在的表(在 MS-SQL / T-Sql 中)

我们在 db 中已经有这个表 ( IPConfig)。(SQL Server 2k8)

IP         |   Member
-----------------------
10.1.2.100 | Joe
10.2.2.200 | Maley
Run Code Online (Sandbox Code Playgroud)

现在,我应该有一个查询应该执行以下操作:

  1. 保持当前表不变。如果表不存在,请创建一个。
  2. 插入新记录(在下一版本部署中定义)

我从这个开始但无法继续。

    IF (NOT EXISTS (SELECT * 
                     FROM INFORMATION_SCHEMA.TABLES 
                     WHERE TABLE_SCHEMA = 'dbo' 
                     AND  TABLE_NAME = 'IPConfig'))
    BEGIN
      CREATE TABLE dbo.IPConfig (
        IP CHAR(10) PRIMARY KEY NOT NULL,
        Member VARCHAR(32) NOT NULL)
    END

    DECLARE @TempTable Table(               -- Create a temp table.
     IP CHAR(10) PRIMARY KEY NOT NULL,
     Member VARCHAR(32) NOT NULL)

   INSERT INTO @TempTable( 
    IP,
    Member)
    SELECT
      '10.1.2.100',   --Already existing …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server triggers

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

如何在system.array中保留null - C#?

我使用以下代码获得了excel表的第一行.

Excel.Range firstRow = ws.UsedRange.Rows[1];
var row = (System.Array) firstRow.Cells.Value;
List<object> Array = row.OfType<object>().ToList();
Run Code Online (Sandbox Code Playgroud)

row 由空值组成.

例如. 在此输入图像描述

问题是当它转换为列表时,所有空值都将丢失. (在此声明之后)

 List<object> Array = row.OfType<object>().ToList();
Run Code Online (Sandbox Code Playgroud)

例如. 在此输入图像描述

任何想法如何防止这种情况发生?

.net c# excel system.array

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

从对象转换为字符串时如何处理null异常?

只是好奇:

这些线投掷Invalid Cast Exception: Unable to cast object of type 'System.Double' to type 'System.String'.

 Object obj = new object();
 obj = 20.09089;
 string value = (string)obj;
Run Code Online (Sandbox Code Playgroud)

obj从a 获得了价值library.

如何简单转换为string我们不知道object枚举时的类型?

.net c# null

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