与类名作为方法的返回值类型 [英] Methods with classname as a return type

查看:467
本文介绍了与类名作为方法的返回值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#或任何类型的编程语言的新的。
当我看到在C#代码,我发现有很多混乱的是在这里。其中一人我想在这里澄清一下。
方法常见的结构是



 <修改><返回类型><方法名称>()
{
//做任何事情
返回< ABC应该同上取代;其声明的返回类型
}

在上面说的例子中,我看到了许多次< ;返回类型> 使用<类名称> <字典>
,但自然这些也是用户定义的类型。所以这也应该在这里。但是,永远不要站在实际上这里发生了什么,当我们在地方字符串或任何数据类型的使用类名?



请让我明白了这方便的话,又容易。例子

 类StockData 
{
公众诠释StockID {搞定;组; };
公共双CurrentPrice {搞定;组; };
公共字符串StockName {搞定;组; };
}

类GetStock
{
公共StockData / *(这也就是上面的类名相同的名称)* / GetStockData(串符号)
{
//什么
返回/ *(会是怎样的返回类型?)* /;
}
}


解决方案

所以,如果我们有一个的 三角方式 GetTriangle 其中有在参数 isBlue

 公共类三角
{
公共字符串颜色{搞定;组; }
}

公共三角GetTriangle(布尔isBlue)
{
三角resultTriangle;

如果(isBlue)
{
resultTriangle =新的三角形{颜色=蓝};
}
,否则
{
resultTriangle =新的三角形{颜色=红};
}

返回resultTriangle;
}

我们可以称之为 GetTriangle 用的参数真正像这样:

 三角blueTriangle = GetTriangle(真); 

三角redTriangle = GetTriangle(假);



这两个 GetTriangle 的结果三角 S,即使它们包含不同的数据(在这种情况下,不同的颜色)。



就像一个孩子的木刻玩具,编译器会检查哪些数据的形,不是它是什么颜色。所以,如果你试图返回广场从它返回一个方法三角则编译器将抛出一个错误,在相同的这样你就不能把一个正方形块三角形孔。


I'm new in c# or any type of programming language. When I see codes in c# I found there are lot of confusions are over here. one of them I want to clarify from here. Methods common structure is

<modifier><return type><method name>()
{
//do any thing
return <abc which should same as return type which declared above>;
}

in above said example I saw many times in <return type> use of <class name>, <dictionary> etc. but natural these are also user defined types. so this also should be here. But, never stood what happened actually here when we use "class name" in place of string or any data type?

Please let me understand this with easy words, and easy examples.

class StockData
{
    public int StockID { get; set; };
    public double CurrentPrice { get; set; };
    public string StockName { get; set; };
}

class GetStock
{
    public StockData /*(this is the same name which is the class name above)*/ GetStockData(string symbol)
    {
        //something
        return /*(what will be the return type?)*/;
    }
}

解决方案

So if we have a class Triangle and a method GetTriangle which has the parameter isBlue.

public class Triangle
{
    public string Colour { get; set; }
}

public Triangle GetTriangle(bool isBlue)
{
    Triangle resultTriangle;

    if (isBlue)
    {
        resultTriangle = new Triangle { Colour = "Blue" };
    }
    else
    {
        resultTriangle = new Triangle { Colour = "Red" };
    }

    return resultTriangle;
}

We can call GetTriangle with an argument of true or false like so:

Triangle blueTriangle = GetTriangle(true);

Triangle redTriangle = GetTriangle(false);

Both of the results of GetTriangle are Triangles, even though they contain different data (in this case a different Colour).

Like a child's wood-block toy, the compiler checks what the "shape" of the data is, not what colour it is. So if you try to return a Square from a method which returns Triangle then the compiler will throw an error in the same way you wouldn't be able to put a square block in a triangle hole.

这篇关于与类名作为方法的返回值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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