C#:创建方法时何时使用void? [英] C#: When to use void while creating a method?

查看:262
本文介绍了C#:创建方法时何时使用void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我正在尝试在这里实现示例代码,我只想问:



1.每当我创建一个类时,默认情况下,如果类名是Car1,它会自动拥有公共类Car1并且在其中,第一个方法是public Car1。为什么每当我创建另一种方法时,我们都会说:Car2在公共类Car1中。但是除了公共Car1的大括号外,它说方法必须有一个返回类型?



Hey guys,

I'm trying to implement a sample code here, I just want to ask:

1. Whenever I create a class, by default if the class name is Car1, it automatically has "public class Car1" and inside it, the first method "public Car1". How come, that whenever I create another method let's say: Car2 inside "public class Car1". But out side the curly bracket of "public Car1" it says "Method must have a return type"?

public class Car1{
public Car1()

{
//Stuff here
}

public car2()    <<<===============This will cause an error method must have a return type, unless I put  void before the car2
{
//Stuff here
}

}





你去,我希望有人可以告诉我,因为我正在尝试创建一个新方法,将数据发送到另一个类在另一个文件中,我不确定我是否可以使用void来做到这一点。



T hanks,



There you go, I hope someone can tell me because I'm trying to create a new method that will send a data to another class in another file and I'm not sure if I will be able to do that with the use of void.

Thanks,

推荐答案

这里你需要了解一些事情。这些是



1.构造函数:它的名称与类名相同,并且没有任何返回类型。

2.方法:每种方法都有一个返回类型。如果你没有从方法中返回任何值,那么它是 void type。

3. void:Void是一个返回类型并且它被使用当方法没有返回时。



在你的例子中,Car1是你的类所以它的构造函数是



public car1()

{

}



但是Car2()不是Car1的构造函数。这是Car1的一种方法,所以它应该是一个有效的返回类型。



检查以下示例:



Here You need to know fewthings. These are

1. Constructor : It's name same as class name and it doesn't have any return type.
2. Method : Each method has a return type. If you don't return any value from a method then it is void type.
3. void : Void is a return type and it used when method doesn't returns.

In your example Car1 is your class so it constructor is

public car1()
{
}

But Car2() is not a constructor for Car1. It is a method for Car1 so it should be a valid return type.

Check following example:

using System;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Car2 car = new Car2(100.12f);
            Console.WriteLine("Speed is :{0}",car.Speed);
            Console.Read();
        }
    }

    public class Car1
    {
        private float mSpeed;

        public Car1(float speed)
        {
            mSpeed = speed;
        }
        public float Speed
        {
            get { return mSpeed; }
        }
    }
    public class Car2 : Car1
    {
        public Car2(float speed)
            : base(speed)
        {
        }

    }

}


请完成这些主题



构造函数 [ ^ ]

c#构造函数 [ ^ ]

C#中的构造函数介绍 [ ^ ]
Pls go through these topics

Constructors[^]
c# Constructors[^]
An Intro to Constructors in C#[^]


构造函数是在创建对象时执行的类的特殊类型的方法。构造函数负责其类的对象初始化和内存分配。它们具有与类相同的名称并且没有返回类型。



不同的构造函数类型如下: />
默认构造函数

参数化构造函数

私有构造函数

静态构造函数

复制构造函数



这里Car1()是构造函数,所以它没有返回类型,Car1()也不带任何参数因此它是默认/非参数化构造函数的一个例子。



Car2()是一种方法,所以它必须有一个返回类型
Constructors are the special types of methods of a class which get executed when it's object is created. Constructors are responsible for object initialization and memory allocation of its class.They have same name as of class and have no return type.

Different Constructor types are as :
Default Constructor
Parameterized constructor
Private Constructor
Static Constructor
Copy Constructor

Here Car1() is the constructor so it has no return type , also Car1() doesn't take any argument therefore it is an example of default/non-parameterized constructor .

Car2() is a method so it must have a return type


这篇关于C#:创建方法时何时使用void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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