using System;
using System.Collections.Generic;
using ConsoleApp3;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Customers customers = new Customers();
List<Customers> names = new List<Customers>()
{
new Customers {Names = "Tanveer"},
new Customers {Names = "Nabila"},
new Customers {Names = "Suraj"}
};
foreach (int i = 0; i < names.Count; i++)
{
Console.WriteLine(customers.Names.Length);
}
}
class Customers
{
public string Names { get; set; }
}
}
}
我想创建客户列表并将其写在控制台上。但是Customers.Names为空。我是编程新手,请帮助。谢谢
答案 0 :(得分:5)
首先,让我们修正该命名。命名非常重要,也是更烦人的部分之一。
const obj = {
key1: 1,
key2: 2,
key3: 3
}
const value = 0
const updated = { ...new Proxy(obj, {
keys() {
return Object.keys(obj)
},
get() {
return value
}
})
}
console.log(obj, updated)
然后您使用以下命令对其进行操作:
//Singular for the class
class Customer
{
//Also Singular, as this can only take 1 name
public string Name { get; set; }
}
//Plural, because it is a collection of Customer Instances.
List<Customer> Customers = new List<Customer>()
{
new Customer {Name = "Tanveer"},
new Customer {Name = "Nabila"},
new Customer {Name = "Suraj"}
};
如果您确实希望有一个正在运行的计数器,这就是循环的样子:
//Use a proper foreach, no need to deal with Indexes here
foreach (Customer current in Customers){
Console.WriteLine(current.Name);
}
您在代码中拥有的是for和foreach语法的混用,我怀疑已编译。
答案 1 :(得分:1)
const ffMatchupSchema = new Schema({
teams: {type: [
{
owner_id: {type: Schema.Types.ObjectId, ref: 'Owner'},
ff_teams: {type: {}},
wins: {type: Number},
isFinal: {type: Boolean},
starter_points: {type: Number}
}], required: false},
week: {type: Number},
winner: {type: {}},
matchup_id: {type: Number},
isFinal:{type: Boolean},
isPlayoff: {type: Boolean},
playoffType: {type: String},
playoffTeams: {type: [mongoose.Mixed]}
})
答案 2 :(得分:0)
func sayHello(_ name:String, _ age:Int) -> String {
return "hello \(name) your are \(age) years old"
}
var msg = sayHello("Ben",6)
print(msg)
为空,因为customers.Names
是一个您没有填充任何数据的对象,并且没有明显的用途。 customers
是其中包含有用信息的实际客户列表。
names
也不直接具有Names属性。列表中的对象可以。因此,您需要在特定列表中引用特定对象。
由于您正处于这样做的循环中,因此names
无疑是您的意图。
但是,在循环定义中,它必须是names[i].Names.Length
而不是for
-foreach
使用的语法是不同的。为了使您甚至可以看到foreach
的输出,本不应该进行编译,所以也许这只是您发布的代码中的错字。
null
应该更接近您的需求(尽管我不相信您确实打算打印每个名称的长度...但这取决于您)。
P.S。您可能应该修改命名约定,以便将for (int i = 0; i < names.Count; i++)
{
Console.WriteLine(names[i].Names.Length);
}
作为类型并将Customer
作为属性。如果单数而不是复数,则更易于理解和理解。