我在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(编译器必须从初始化语句右侧的表达式推断变量的类型).
我有一些澄清问题,他们在下面.
提前致谢.
我有点能够理解工厂模式并想出了这个实现。
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) 我正在尝试为我的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的一个问题,它不明白我遵循这个单例模式?
要么
我根本不遵循单身人士模式吗?
我正在看这个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) 我的网址是这样的,
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 …
它更为人所知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.有什么想法吗?
如果在这种情况下发生多个字符串的连接会发生什么?
刚开始Star UML绘制一个class diagram.
与任何应用程序一样,MyBLL(业务层)Model在与DAO(数据库层)交互之后创建一个实例.
或多或少,简化(为清晰起见)类图如下所示:

很明显,BLL完成后将不再使用模型类.
我应该在这里使用什么?aggregation或composition映射MyBLL到MyModel.
我使用了一个composition从关系MyBLL到MyDAO.我的逻辑是在BLL图层存在之后,MyDAO将不再存在,因为它被引用MyBLL.它是否正确?
注意:这是WebAPI我使用C#创建的项目.
我们在 db 中已经有这个表 ( IPConfig)。(SQL Server 2k8)
IP | Member
-----------------------
10.1.2.100 | Joe
10.2.2.200 | Maley
Run Code Online (Sandbox Code Playgroud)
现在,我应该有一个查询应该执行以下操作:
我从这个开始但无法继续。
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) 我使用以下代码获得了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)
例如. 
任何想法如何防止这种情况发生?
只是好奇:
这些线投掷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枚举时的类型?