C#基本类型或复杂类型的方法签名? [英] C# Primitive Types or Complex Types as Method Signatures?

查看:267
本文介绍了C#基本类型或复杂类型的方法签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用Primitve类型或复杂类型的优点和缺点是什么?



当你应该使用基本类型在复杂类型,反之亦然?



如:

 公共类Person 
{
公共字符串名字{搞定;组; }
公共字符串名字{获得;组; }
公众诠释年龄{搞定;组; }
公众诠释智商{搞定;组; }
}


公共无效FooPrimitiveTypes(串名字,字符串姓氏,INT年龄,诠释IQ)
{

}

公共无效FooComplexTypes(人人)
{

}


< DIV CLASS =h2_lin>解决方案


  1. 要通过每个属性,当你在处理不相交值分别通常使用。此外,有时用在构造函数。坏习惯。


  2. 当值与是首选这种方式。




为什么#1是一种不好的做法 - 假设你需要添加高度。我宁愿加入,而不是50方法的另一个特性,更新一个类。


What are the pros and cons of using Primitve Types or Complex Types?

When should you use primitive types over complex types and vice versa?

i.e.:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public int IQ { get; set; }
}


public void FooPrimitiveTypes (string firstName, string lastName, int age, int IQ)
{

}

public void FooComplexTypes(Person person)
{

}

解决方案

  1. To pass each property separately are generally used when you are dealing with disjoint values. Also, sometimes used on constructors. Bad practice.

  2. This way is preferred when the values are related.

Why #1 is a bad practice - suppose you needed to add height. I'd much rather update one class by adding another property, instead of 50 methods.

这篇关于C#基本类型或复杂类型的方法签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆