简体   繁体   English

关于我在C#中实现类的问题

[英]Question on my Class implemention in C#

Currently i have a class which has only properties 目前我有一个仅具有属性的类

class MyEntity
{  
    public string FirstName { get; set; }
    public string LastName  { get; set; } 
}

In my other class, I use to set the values like 在我的另一堂课中,我习惯将值设置为

MyEntity objEnt = new MyEntity();
objEnt.FirstName="abc";
objEnt.LastName = "edf";

The reason i have the class MyEntity with only properties is to ensure adherence. 我拥有仅具有属性的MyEntity类的原因是为了确保遵守。

Question here is, if this approach is correct? 这里的问题是,这种方法是否正确? I can have multiple instances of MyEntity 我可以有多个MyEntity实例

Can i use Static Class for this? 我可以为此使用静态类吗? How can i use Generics in this? 我该如何使用泛型?

BTW the implementation is in C# 顺便说一句,实现是在C#中

Thanks in advance 提前致谢

Cheers, 干杯,

Karthik 卡尔提克

  • You can't use a static class, as you have state. 由于具有状态,因此不能使用静态类。 Static classes can only consist of static members. 静态类只能由静态成员组成。
  • It's hard to see how you'd use generics here 很难在这里看到如何使用泛型

Personally I prefer types to be immutable where possible - for example by giving MyEntity a constructor with two parameters, and removing the public setters from the properties. 就个人而言,我希望类型尽可能地保持不变-例如,通过为MyEntity提供带有两个参数的构造函数,并从属性中删除公共设置器。 (Whether you keep private setters which you just don't use outside the constructor, or move to "proper" readonly fields and readonly properties is another choice.) However this mutable type may be absolutely fine for you - it's impossible to say without knowing what you're trying to do. (不管你保持你只是不构造以外使用,或转移到私人二传手“适当的”只读域和只读属性是另一种选择。)但是这种可变类型可能是你绝对罚款-这是不可能不知道该说些您正在尝试做什么。

Note that you can use object initializers to make your initialization simpler, while you're using the mutable type: 请注意,在使用可变类型时,可以使用对象初始化程序简化初始化过程:

MyEntity objEnt = new MyEntity { FirstName = "abc", LastName = "edf" };

(If you made the type immutable, you'd pass the values in the constructor. You could still use names via C# 4's "named arguments" feature, if you're using C# 4.) (如果将类型设为不可变的,则可以在构造函数中传递值。如果使用的是C#4, 仍可以通过C#4的“命名参数”功能使用名称。)

As already said it depends from your needs. 如前所述,这取决于您的需求。 Just to note that you can't use static class as you want to have multiple instances and also there can be issues with synchronization while access from multiple threads. 只是要注意,您不能使用静态类,因为您要拥有多个实例,并且在从多个线程进行访问时同步可能会出现问题。

As for generics ... whats for you need it here ? 至于泛型……您在这里需要什么?

I had an answer half typed, but then Jon answered so my answer in its current form became pointless.... 我输入了一半的答案,但乔恩回答了,所以我的当前形式的答案变得毫无意义。

I will offer a counterpoint on Jon's answer though. 不过,我将对乔恩的回答提出反驳。 Think carefully before using immutable objects - they are good from some perspectives, but you will quickly run into trouble if using them for binding to editable fields, or if serializing them (either to storage or for transferring, ie WCF web service calls). 在使用不可变对象之前要仔细考虑-从某些角度来看,它们是好的,但是如果将它们用于绑定到可编辑字段或序列化它们(存储或传输,即WCF Web服务调用),则将很快陷入困境。

Here is a quick and dirty use of generics for your unspecified DTO I am assuming. 我假设这是对您未指定的DTO的泛型快速使用。

class MyEntity<T>
{
   public T value { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM