C# using class data type


  • 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();
                foreach(Car car in myArrayList)
        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; }

    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.


    • Edited by k9nneo1191 Monday, April 10, 2017 3:16 PM wrong code
    Monday, April 10, 2017 3:14 PM

All replies

  • 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.

    Very very simply:

    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).

    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.

    But really, just consider:

    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.

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

    And so on...

    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.

    • Proposed as answer by Ryan Software Monday, April 10, 2017 4:51 PM
    Monday, April 10, 2017 3:34 PM