C#使用类数据类型 [英] C# using class data type

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

问题描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication43
{


    class Program
    {
        static void Main(string[] args)
        {
            Car Car1 = new Car();
            Car1.Make = "Oldsmobile";
            Car1.Model = "Cutlas Supreme";

            Car Car2 = new Car();
            Car2.Make = "Geo";
            Car2.Model = "Prism";

            Book b1 = new Book();
            b1.Author = "Robert  Tabor";
            b1.Title = "Microsoft .NET XML Web Service";
            b1.ISBN = "0-000-00000-0";


            ArrayList myArrayList = new ArrayList();
            myArrayList.Add(Car1);
            myArrayList.Add(Car2);
            

            foreach(Car car in myArrayList)
            {
                Console.WriteLine(car.Make);
            }
            Console.ReadLine();
        }
    }

    class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
    }


    class Book
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public string ISBN { get; set; }
    }
}

我正在尝试学习c#.我无法弄清作为数据类型或返回类型的类是如何工作的.可以请一个人告诉,展示或教我它是如何工作的.导致im不像使用内置数据类型(字符串,int,char等)那样使用类数据类型.

Im trying to learning c#. I cant figure out how does a class as a data type or return type works. Can some one please tell,show or teach me how it works. Cause im not used of using class data types as like using the build-in data types (string,int,char,etc).

何时以及为什么要使用类数据类型.

 

when and why should i use class data type.

 

推荐答案

最好首先了解一切是数据类型. 类"和数据类型"之间没有任何真正的区别.

It may be best to first understand that everything is a data type. There isn't any real difference between 'class' and 'data type'.

尽管有两种根本不同的数据类型:值类型和引用类型.如果您只是刚刚开始学习,那么这是一个相当高级的主题,但可能值得关注 链接并对其进行阅读.

There are two fundamentally different kinds of data type though: value types and reference types. This is a reasonably advanced topic if you are only just started to learn, but may be worth following this link and giving it a read.

非常简单:

值类型是简单"类型,例如整数和字符,不需要显式创建,并且当从一个变量分配给另一个变量时,将复制该类型的整个值"(因此即为名称).

Value types are 'simple' types like integers and chars that don't require explicit creation and when assigned from one variable to another the entire 'value' of the type is copied (hence the name).

引用类型趋于复杂一些,通常需要使用'new'创建(尽管可以是静态的),并且当从一个变量复制到另一个变量时,您实际上只是在复制对它的引用(因此而得名) .实际数据浮动在 一个特殊的内存区域.

Reference types tend to be a bit more complex, usually needs to be created with 'new' (though can be static) and when copied from one variable to another you are only really copying a reference to it (hence the name). The actual data is floating about in a special memory area.

但实际上,请考虑:

整数是表示单个整数的简单类/类型.

An integer is a simple class/type that represents a single whole number.

字符串是代表字符有序集合的类/类型.

A string is a class/type representing an ordered collection of characters.

汽车是一种类/类型,代表具有Make和Model属性的汽车.

A Car is a class/type representing, well, a car with the properties of Make and Model.

依此类推...

Car和Book是您定义的类,而不是.Net内置的类,这一事实无关紧要.方法可以接受任何类型作为参数并返回类型-无论是整数,字符,字符串,Car还是Book,都无关紧要.

The fact that Car and Book are classes you defined rather than built-in to .Net doesn't matter. A method can accept any type as a parameter and return a type - doesn't matter whether it is an integer, a char, a string, a Car or a Book.


这篇关于C#使用类数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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