本文介绍了并非所有代码路径都返回一个值,任何一个请提供解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ProductModel
{
public string InsertProduct(Product product)
{
try
{
GarageEntities db = new GarageEntities();
db.Products.Add(product);
db.SaveChanges();
return product.Name + "was succesfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
推荐答案
public string InsertProduct(Product product)
{
try
{
GarageEntities db = new GarageEntities();
db.Products.Add(product);
db.SaveChanges();
// May be moved to end of function
return product.Name + "was succesfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
// Make the compiler happy or return success here
return "";
}
询问此类问题的提示:
始终包含编译器代码(此处为CS0161),因为这样可以清楚地说明发生了什么。但是,在网上搜索消息和/或代码也会回答这个问题。
A tip for asking about such problems:
Always include the compiler code (CS0161 here) because this makes it clear what happened. However, searching the web for the message and/or code would have answered this too.
这篇关于并非所有代码路径都返回一个值,任何一个请提供解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!