我知道在C#中使用Null合并运算符的标准方法是设置默认值.
string nobody = null;
string somebody = "Bob Saget";
string anybody = "";
anybody = nobody ?? "Mr. T"; // returns Mr. T
anybody = somebody ?? "Mr. T"; // returns "Bob Saget"
Run Code Online (Sandbox Code Playgroud)
但还有什么可以??用于?它不像三元运算符那样有用,除了比以下更简洁和更容易阅读:
nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // returns Bob Saget
Run Code Online (Sandbox Code Playgroud)
所以考虑到甚至更少知道空合并运算符......
你有没有用过??别的东西?
是??必要的,还是应该只使用三元运算符(大多数人都熟悉)
c# null coding-style conditional-operator null-coalescing-operator
如果有时间使用标准linq关键字或linq扩展方法与lambda表达式,我正试图处理.他们似乎做同样的事情,只是写得不一样.这纯粹是风格问题吗?
var query = from p in Products
where p.Name.Contains("foo")
orderby c.Name
select p;
// or with extension methods:
var query = Products
.Where(p => p.Name.Contains("foo"))
.OrderBy(p => p.Name);
Run Code Online (Sandbox Code Playgroud)
他们非常相似,第二个例子更简洁,但如果你不知道=>在做什么,可能会表现得更差.
除了编写简洁的代码之外,使用扩展方法而不是LINQ语法还有其他优点吗?
我有一个包含所有数据库逻辑的类库.我的DAL/BLL.
我有一些Web项目将使用相同的数据库和类,所以我认为将数据层抽象到自己的项目中是一个好主意.
但是,当为某些项目的类添加功能时,我想向某些类添加方法.
例如,我的数据层有Product和SomeItem对象:
// Data Access Layer project
namespace DAL {
public class Product {
//implementation here
}
public class SomeItem {
//implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
在一个项目中,我想添加一个由不同内容项使用的接口,所以我有一个名为的类:
// This is in Web Project
namespace DAL {
public partial class Product : ICustomBehaviour {
#region ICustomBehaviour Implementation
TheSharedMethod();
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
使用相同的命名空间在单独的项目(创建依赖项)中编写部分类是一个好主意吗?如果这是一个坏主意,我怎样才能使这种类型的功能工作?
它似乎不想在编译时合并它们,所以我不确定我做错了什么.
我知道这可能是意见,但我正在寻找最佳实践.
据我所知,IQueryable<T>实现IEnumerable<T>,所以在我的DAL中,我目前有方法签名,如下所示:
IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProductsByCategory(int cateogoryId);
Product GetProduct(int productId);
Run Code Online (Sandbox Code Playgroud)
我应该IQueryable<T>在这里使用吗?
这两种方法的优点和缺点是什么?
请注意,我打算使用Repository模式,所以我将有一个这样的类:
public class ProductRepository {
DBDataContext db = new DBDataContext(<!-- connection string -->);
public IEnumerable<Product> GetProductsNew(int daysOld) {
return db.GetProducts()
.Where(p => p.AddedDateTime > DateTime.Now.AddDays(-daysOld ));
}
}
Run Code Online (Sandbox Code Playgroud)
我应该改变我IEnumerable<T>的IQueryable<T>吗?一个或另一个有哪些优点/缺点?
在定义使用不同过滤器返回相同形状数据的多个方法时,有什么更好的做法?显式方法名称或重载方法?
例如.如果我有一些产品,我从数据库中提取
明确的方式:
public List<Product> GetProduct(int productId) { // return a List }
public List<Product> GetProductByCategory(Category category) { // return a List }
public List<Product> GetProductByName(string Name ) { // return a List }
Run Code Online (Sandbox Code Playgroud)
重载方式:
public List<Product> GetProducts() { // return a List of all products }
public List<Product> GetProducts(Category category) { // return a List by Category }
public List<Product> GetProducts(string searchString ) { // return a List by search string }
Run Code Online (Sandbox Code Playgroud)
我意识到你可能会遇到类似签名的问题,但是如果你传递的是对象而不是基类型(string,int,char,DateTime等),这将不再是一个问题.所以...是一个好主意,重载的方法来减少你有方法和清晰的数字,或者应该 …
有什么情况throw可以避免可以避免的错误吗?
我正在考虑DivideByZeroException和ArgumentNullException
例如:
double numerator = 10;
double denominator = getDenominator();
if( denominator == 0 ){
throw new DivideByZeroException("You can't divide by Zero!");
}
Run Code Online (Sandbox Code Playgroud)
有没有理由抛出这样的错误?
注意:我不是在谈论捕捉这些错误,而是专门知道是否有充分理由抛弃这些错误.
只是重新评估:
我知道在我给你的例子中你可能会更好地处理错误.也许这个问题应该改写一下.是否有任何原因导致throw其中一个错误,而不是在此位置处理它.
c# exception-handling argumentnullexception dividebyzeroexception
很久以前,我正在维护一个由外部公司用VB Script编写的经典ASP应用程序.
我有一个图像文件路径数组,如下所示:
dim banners, arrKeys, i
set banners=CreateObject("Scripting.Dictionary")
banners.Add "banner1.jpg", "http://www.somelink.com"
banners.Add "banner2.jpg", "http://www.somelink.com"
banners.Add "banner3.jpg", "http://www.somelink.com"
Run Code Online (Sandbox Code Playgroud)
这仅存在于包含横幅广告的网页上.有一些标准代码在包含文件中遍历此列表(所有页面都是通用的).
If Not banners Is Nothing then
' then loop through the Dictionary and make a list of image links
End if
Run Code Online (Sandbox Code Playgroud)
问题是如果banners没有在页面上实例化(它不在所有页面上),我会收到Can't find object错误
检查VB脚本中是否存在对象的正确方法是什么?
一个数组(一行元素):
[ ][ ][ ][ ][ ][ ]
Run Code Online (Sandbox Code Playgroud)
二维数组(表):
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
Run Code Online (Sandbox Code Playgroud)
三维数组:
//Imagine the above table as a cube ( a table with depth )
Run Code Online (Sandbox Code Playgroud)
如何可视化4-D阵列?
我最接近的是多个立方体,因此int[,,,][5,10,2,7]将是立方体5,第10行,第2列,层(深度)7.
我不确定这是否是可视化4-D阵列的最佳方式,但是......我不确定这是教它的最佳方式......但它确实具有可扩展性的优势(a行立方体,立方体表,立方体立方体(6-d阵列)
时间立方体是我能想到的另一种方式.
我在这里走在正确的轨道上吗?
我不一定需要在服务器上运行它,但是,我想使用~/js/somefile.js语法.
以前,我刚刚使用绝对路径设置了所有内容,并将我的项目设置为根级别.所以,我只是声明我所有的样式表,背景图片和javascript文件/css/somefile.css
但是,对于此项目,它不以root身份运行.
我不能加上runat="server"脚本标签.
不过,我可以把它放在链接标签上.
这必须是一个常见的问题,只有一些简单的答案.
我做的很多是从AppSettings读取整数.最好的方法是什么?
而不是每次都这样做:
int page_size;
if (int.TryParse( ConfigurationManager.AppSettings["PAGE_SIZE"], out page_size){
}
Run Code Online (Sandbox Code Playgroud)
我在想我Helpers班上的一个方法是这样的:
int GetSettingInt(string key) {
int i;
return int.TryParse(ConfigurationManager.AppSettings[key], out i) ? i : -1;
}
Run Code Online (Sandbox Code Playgroud)
但这只是为了节省一些按键.
理想情况下,我喜欢将它们全部放入某种我可以使用intellisense的结构中,因此我不会遇到运行时错误,但我不知道我是如何处理这个...或者如果这是可能的话.
从Web.Config的AppSettings部分获取和读取整数的最佳实践方法是什么?
还有一件事...
设置它不是一个好主意readonly吗?
readonly int pageSize = Helpers.GetSettingInt("PAGE_SIZE") 似乎不起作用.
c# ×5
.net ×1
appsettings ×1
asp-classic ×1
asp.net ×1
coding-style ×1
datacontext ×1
iqueryable ×1
java ×1
linq ×1
linq-to-sql ×1
methods ×1
null ×1
object ×1
overloading ×1
runatserver ×1
script-tag ×1
vbscript ×1
web-config ×1