partial class Employee
{
protected string empName;
protected int empID = new int();
protected float currPay;
protected static int empAge;
protected string empSNN;
// these are nested classes
public class BenefitPackage
{
// I want to access this class
public enum BenefitPackageLevel
{
standard,Gold,Platinum
}
public double ComputePayDeduction()
{
return 125.0;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过employee类的实例访问BenefitPackageLevel类,例如:
Employee emp= new Employee()
var benefitpackage= emp.BenefitPackage.BenefitPackageLevel.standard;
Run Code Online (Sandbox Code Playgroud)
但是为什么我没有将BenefitPackage定义为静态成员,却可以通过类级别访问它,例如:
Employee.BenefitPackage.BenefitPackageLevel.standard
Run Code Online (Sandbox Code Playgroud)
默认情况下,嵌套类是否可能是静态的?
我正在尝试将应用程序配置为使用我的类(派生自DbContext)ApplicationDbContext连接到我的数据库。我已经制作了配置文件appsetting.json:
"Data": {
"SportStoreProducts": {
"ConnectionStrings":"Server=(localdb)\\MSSQLLocalDB;Database=SportStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
我使用依赖注入通过 Startup 构造传递对象实现IConfiguration(即appsetting.json):
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想在ConfigureServices方法中使用这个配置文件Startup并使用扩展方法AddDbContext来注册我ApplicationDbContext使用SportStore我在配置文件中分配的SQL数据库():
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(***);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我应该将什么UseSqlServer作为参数 (***)传递给方法,以便它可以使用我提供的配置属性将上下文连接到 SQL Server 数据库?
我加了一些key,value字典的array,现在当我试图让我得到了一些错误的所有对:
这是代码:
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue.ToString() == "January")
{
Dictionary<string, int>[] temperatures = new Dictionary<string, int>[2];
temperatures[0]=new Dictionary<string, int>();
temperatures[1]=new Dictionary<string, int>();
temperatures[0].Add("Day1",22);
temperatures[1].Add("Day2",23);
foreach (KeyValuePair<string,int> temperture in temperatures[])
{
Label1.Text = string.Format("at {0} the temperture is {1} degree", temperture.Key, temperture.Value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在错误或问题是这一行:
foreach (KeyValuePair<string,int> temperture in temperatures[])
Run Code Online (Sandbox Code Playgroud)
如果我写,temperatures[]我收到错误消息:
语法错误,值预期
索引器有1个参数,但它调用0参数()
如果我添加一个像temperatures[0]或的索引temperatures[1],我只得到第一或第二项的关键和值,但不是全部.
我该怎么办?
我的问题是为什么我们需要在string []中检查null错误.是不是字符串类型默认接受空值?那么为什么我们应该通过使用Null条件运算符标记(?)来检查string []是否包含null.
例如看看这个方法:
它来自C#和NetFramework 4.6
static void TesterMethod(string[] args)
{
// We should check for null before accessing the array data!
Console.WriteLine($"You sent me {args?.Length} arguments.");
}
Run Code Online (Sandbox Code Playgroud)
我的意思是,如果我们可以为字符串赋值null为什么我们应该检查null错误?为什么要抛出错误?
Console.WriteLine($"You sent me {args.Length} arguments.");
Run Code Online (Sandbox Code Playgroud) 对于能够使用" foreach " 进行迭代的类,它应该实现IEnumerable接口.但为什么这个没有实现的简单类可以迭代?
public class Person
{
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person() { }
public Person(string firstName, string lastName, int age)
{
Age = age;
FirstName = firstName;
LastName = lastName;
}
public override string ToString()
{
return string.Format("Name: {0} {1}, Age: {2}",
FirstName, LastName, Age);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我没有得到关于使用Foreach的错误
class Program
{
static void Main(string[] args)
{ …Run Code Online (Sandbox Code Playgroud)