Jump to content
  • Advertisement
Sign in to follow this  
acddklr07

Question about C# regarding arrays and classes

This topic is 4016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a question regarding arrays and classes in C#. Right now, I am currently learning C# and I saw that you can make arrays of classes. For example, if I make a class name Students I can make an array with it. I decided to try a little experiment with it. I made a int named num and I had the user enter the number of students. I then used the num int to set the number of students to make. Below is an example:
using System;

class Students
{
    public string name;
    public int age;
    public string sex;
}

class Program
{
    static void Main()
    {
        Console.Write("Please enter the number of students you want: ");
        int num = int.Parse(Console.ReadLine());

        Students[] class1 = new Students[num];

        for (int i = 0; i < num; i++)
        {
            class1 = new Students();

            Console.Write("Please enter a name: ");
            class1.name = Console.ReadLine();
            Console.WriteLine();

            Console.Write("Pleasse enter a age: ");
            class1.age = int.Parse(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Please enter a gender: ");
            class1.sex = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine();
        }

        for (int i = 0; i < num; i++)
        {
            Console.WriteLine(class1.name);
            Console.WriteLine(class1.age.ToString());
            Console.WriteLine(class1.sex);
        }

    }
}

I was wonder is it safe to do it. I previously learned C++ and I believed you couldn't do that, even with regular arrays so I wanted to make sure it is safe to do it. Also, I have another question. I am using .Net Book Zero and it had said that structs and classes in C++ can both have methods/functions in it, but I remember reading a book that said structs in C++ can only have variables and no functions. Is it true what the author said? ~Carl J. Loucius

Share this post


Link to post
Share on other sites
Advertisement
I'm not exactly sure what you mean when you say "is it safe to do that". Cause compiler errors or possible memory leakage? Well, definitely no memory leakage, that's an advantage you have with C# over C++. As far as actually being able to do it, yes, you can make an array of a specific Class.

ClassName[] foo = new ClassName[num];

(Something like that, it's been awhile since C#, Ruby now)

I think the array itself stores references to the actual data, too.

I can't answer your question about structs having methods, I don't think they can, though. But C# doesn't let you do very many unsafe things, it's rather beginner friendly (not saying you're a beginner).

Share this post


Link to post
Share on other sites
It is perfectly safe. An equivalent thing can be done in C++, but only with a little more effort (if using an appropriate standard library container - the C# library has these concepts as well, and they are useful in C# as well even though its arrays are also more powerful) or considerably more effort (if trying to actually use "an array" in the C++ sense).

Structs in C++ certainly can have member functions: they are the same thing in C++ as classes. They merely provide different defaults for public vs. private access (in C++ you indicate public/private/protected "sections" with labels, rather than specifying it separately for each member). In C#, they can (AFAIK; see the C# workshop forum for a more authoritative answer) have member functions, but the behaviour of classes and structs differs (reference vs. value semantics). In C, you can't have member functions in a struct, and there are no "classes" at all.

Share this post


Link to post
Share on other sites
IIRC Plain C Struct didn't support Methods. C++ Struct Yes

Also It's not an array of class but an array of Objects. A class is only a definition and the object is an instance of a class.

There is nothing dangerous in your code currently. You work with index and the only thing that could happen is that you mess up your code and try to manipulate an object with a non-existing index.

And I don't know what you mean by you couldn't do that in C++.

If it's dynamic size array, the answer is yes.
If it's Object array, the answer is yes ( Look at Vector )

Also you could use the generics for your array instead of this kind of array.

Using System.Collections.Generic

List<Student> myStudentList = new List<Student>();

This will create you a list of student and you don't need to specify the size of the array. Just keep doing myStudentList.Add(new Student());

Edit: Zahlman types too fast for me =/

Edit2: Yeah C# Struct are allocated on the stack and will be destroyed when the object reach the end of it's scope, unlike class which are on the managed heap and destroyed on garbage collection.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
It is perfectly safe. An equivalent thing can be done in C++, but only with a little more effort (if using an appropriate standard library container - the C# library has these concepts as well, and they are useful in C# as well even though its arrays are also more powerful) or considerably more effort (if trying to actually use "an array" in the C++ sense).

Trivial really for both, assuming you apply existing libraries instead of trying to reinvent the wheel.
Quote:
Structs in C++ certainly can have member functions: they are the same thing in C++ as classes. They merely provide different defaults for public vs. private access (in C++ you indicate public/private/protected "sections" with labels, rather than specifying it separately for each member). In C#, they can (AFAIK; see the C# workshop forum for a more authoritative answer) have member functions, but the behaviour of classes and structs differs (reference vs. value semantics). In C, you can't have member functions in a struct, and there are no "classes" at all.

Yes, structures in C# can have member variables, they can also implement interfaces. They cannot derive from other classes nor structures and default to being allocated on the stack (unless as a member of a reference type, in which case they are counted as being part of the size of the object).

As far as the non-member functions in C goes... sure, but you can do things like

enum { False, True } f(struct S {int i; char c;} s) {
}

Which always makes me giggle.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Yes, structures in C# can have member variables, they can also implement interfaces. They cannot derive from other classes nor structures and default to being allocated on the stack (unless as a member of a reference type, in which case they are counted as being part of the size of the object).


Huh. I would have expected them to be able to derive privately (and without generating dynamic dispatch machinery) from other structs. I guess they didn't want to encourage the use of inheritance where composition is indicated? :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Washu
Yes, structures in C# can have member variables, they can also implement interfaces. They cannot derive from other classes nor structures and default to being allocated on the stack (unless as a member of a reference type, in which case they are counted as being part of the size of the object).


Huh. I would have expected them to be able to derive privately (and without generating dynamic dispatch machinery) from other structs. I guess they didn't want to encourage the use of inheritance where composition is indicated? :)

Private/protected inheritance in C++ is a crusty thing that should optimally be removed in favor of explicit composition. After all, that's what you are doing when you inherit from a class using private/protected.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!