在编程接口时,我发现我正在进行大量的转换或对象类型转换.
这两种转换方法有区别吗?如果是,是否有成本差异或这对我的计划有何影响?
public interface IMyInterface
{
void AMethod();
}
public class MyClass : IMyInterface
{
public void AMethod()
{
//Do work
}
// Other helper methods....
}
public class Implementation
{
IMyInterface _MyObj;
MyClass _myCls1;
MyClass _myCls2;
public Implementation()
{
_MyObj = new MyClass();
// What is the difference here:
_myCls1 = (MyClass)_MyObj;
_myCls2 = (_MyObj as MyClass);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,什么是"一般"首选方法?
我有一个Web服务,我正在尝试进行单元测试.在服务中,它从HttpContext
类似物中提取了几个值,因此:
m_password = (string)HttpContext.Current.Session["CustomerId"];
m_userID = (string)HttpContext.Current.Session["CustomerUrl"];
Run Code Online (Sandbox Code Playgroud)
在单元测试中,我使用简单的工作者请求创建上下文,如下所示:
SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
HttpContext context = new HttpContext(request);
HttpContext.Current = context;
Run Code Online (Sandbox Code Playgroud)
但是,每当我尝试设置值时 HttpContext.Current.Session
HttpContext.Current.Session["CustomerId"] = "customer1";
HttpContext.Current.Session["CustomerUrl"] = "customer1Url";
Run Code Online (Sandbox Code Playgroud)
我得到null引用异常,表示HttpContext.Current.Session
为null.
有没有办法在单元测试中初始化当前会话?
我遇到了一个我正在努力解决的DataRow问题.
使用OleDbConnection从Excel电子表格中读取数据行.
如果我尝试使用列名从DataRow中选择数据,即使存在数据,它也会返回DBNull.
但它并不那么简单.
datarow.Table.Columns[5].ColumnName
返回"我的专栏".
datarow["my column"]
返回DBNull.
datarow[5]
返回500.
datarow[datarow.Table.Columns[5].ColumnName]
返回DBNull.(只是为了确保它不是一个错字!)
我可以使用列号从数据行中选择一些东西,但我不喜欢这样做,因为如果列顺序改变,软件将会中断.
我需要将nullable int转换为string
int? a = null;
string str = a.toString();
Run Code Online (Sandbox Code Playgroud)
如何毫无例外地执行此操作?我需要将字符串设置为"Null".请指导我提前谢谢.
你好
继续这个问题转换和解析有什么区别?
这是两行代码.
Convert.ToString(myObject);
myObject.ToString();
Run Code Online (Sandbox Code Playgroud)
我的问题是有什么区别,最好用哪个?
先感谢您.
这两种方法有什么区别吗?
static void Main(string[] args)
{
object str = null;
string test1 = str?.ToString() ?? "";
string test2 = Convert.ToString(str);
}
Run Code Online (Sandbox Code Playgroud)
编辑1:
正如@Fabio指出的那样,我们有第三种可能的方法:
string test3 = $"{str}";
Run Code Online (Sandbox Code Playgroud) 我有以下代码从excel文件中读取数据,我遇到的问题是,当我尝试读取范围数据时,我得到一个异常"无法在此行上对空引用执行运行时绑定"
if (Int32.TryParse(xlRange.Cells[1, 4].Value2.ToString(), out value))
Run Code Online (Sandbox Code Playgroud)
我想知道的是我如何正确访问该范围内的信息.提前致谢
void ReadFromExcelToGrid2(String fileNameString)
{
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileNameString);
//get list of worksheets associated with the current workbook
Microsoft.Office.Interop.Excel.Sheets worksheets = xlWorkbook.Worksheets;
//list the work books in a dropdownlist
IDictionary<int, String> sheets = new Dictionary<int, String>();
foreach (Microsoft.Office.Interop.Excel.Worksheet displayWorksheet in xlWorkbook.Worksheets)
{
sheets.Add(displayWorksheet.Index, displayWorksheet.Name);
}
cmbWorksheets.DataSource = new BindingSource(sheets, null);;
cmbWorksheets.DisplayMember = "Value";
cmbWorksheets.ValueMember = "Key";
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[4]; // assume it is the first sheet …
Run Code Online (Sandbox Code Playgroud) 一个问题...我在下面的示例代码...我要插入直接从数据row..I一些值到database..but不想使用variables.So我的问题是,是否有检查数据行中的值是否存在或者是否为null的方法,如果该值不存在,我必须插入null或者如果它为null则只插入null ...
这个例子:
myQuery = " INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01) " +
" VALUES (" + Lidoc_num + ", '" +
PSEG_NUM + "','" +
SEG_NUM + "','" +
dr_art_custsuply["Id"] + "','" +
dr_art_custsuply["GLN"] + "','" +
dr_art_custsuply["Name"] + "')";
Run Code Online (Sandbox Code Playgroud)
这是我不想用的方式......
if (!dr_art_custsuply.Table.Columns.Contains("GLN") || dr_art_custsuply["GLN"].ToString().Length <= 0)
{
gln = "";
}
else
{
gln = dr_art_custsuply["GLN"].ToString();
}
Run Code Online (Sandbox Code Playgroud) 我想在变量中获取'int'值并将其设置在textBox中.此代码的第二行显示错误:
此表达式不能用作分配目标.
我该如何解决?
int nextCode = teacherManagerObj.GetCode();
//shows error "This expression cannot be used as an assignment target"
Convert.ToInt32(codeTextBox.Text) = nextCode;
Run Code Online (Sandbox Code Playgroud) 我希望看看是否有一种简单的方法可以使用Code Resharper或CodeRush或任何其他工具在整个应用程序中使用Convert.ToString()替换().ToString()?
谢谢
c# ×10
excel ×2
.net ×1
casting ×1
clr ×1
datarow ×1
httpcontext ×1
ms-office ×1
optimization ×1
performance ×1
resharper ×1
string ×1
unit-testing ×1
web-services ×1