Does C# support array of classes?

  • Thread starter Thread starter Andrea Trevisan
  • Start date Start date
A

Andrea Trevisan

I'm following a tutorial tour for Microsoft C# and I ask if
this language supports array of classes like C++.That's a
personal curiosity.
I add the body of the two .cs files of my project which works fine but
commented-out lines:

using System;

namespace My3
{
/// <summary>
/// Summary description for CAMix.
/// </summary>
public class CAMix
{
public CAMix()
{
//
// TODO: Add constructor logic here
//
}
public int i_Y;
}
}

using System;

namespace My3
{
class CMix
{
public int i_Y;
}

struct SMix
{
public int i_Y;
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
CMix c_Mix1 = new CMix();
CMix[] c_Mix2 = new CMix[4];
CAMix[] c_Mix3 = new CAMix[4];

SMix s_Mix1;
SMix s_Mix2 = new SMix();
SMix[] s_Mix3 = new SMix[7];

c_Mix1.i_Y = 8;
Console.WriteLine("c_Mix1.i_Y = " + c_Mix1.i_Y.ToString());
/*for (int i_Index = 0; i_Index < c_Mix2.Length; i_Index++)
c_Mix2[0].i_Y = i_Index * 100;*/
/*for (int i_Index = 0; i_Index < c_Mix3.Length; i_Index++)
c_Mix3[0].i_Y = i_Index * 100;*/

s_Mix1.i_Y = 3;
Console.WriteLine("s_Mix1.i_Y = " + s_Mix1.i_Y.ToString());
s_Mix2.i_Y = 15;
Console.WriteLine("s_Mix2.i_Y = " + s_Mix2.i_Y.ToString());
for (int i_Index = 0; i_Index < s_Mix3.Length; i_Index++)
{
s_Mix3[i_Index].i_Y = i_Index * 10;
Console.WriteLine("s_Mix3[{0}].i_Y = " +
s_Mix3[i_Index].i_Y.ToString(), i_Index);
}
}
}
}

This text editor isn't wonderful,but I rely on your wonderful will.
regards
Mr. Trevisan Andrea
 
Dear Andrea,

I don't know about C++ but C# has Types with which you can do loads of
classy stuff (pun intended).

See my post in the thread "opening form by name" in which you can see
how knowing the name of a form (which is also its class name) is enough to
create and display an instance of the form using Types (and Reflection).

With an array of Types you can do similar things and more for any
class/type.

Hope this piques your curiousity to study Reflection - it's very
powerful.

Best wishes,
Fergus
 
Hi Fergus,

I believe what Andrea wanted is "array of objects", or "array of class
instances." It is different from "array of Type objects", which I admit is
very useful. Thanks,

- Zhanyong Wan

Visual Studio and .NET Setup

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included samples (if any) is subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
--------------------
| From: "Fergus Cooney" <[email protected]>
| References: <us#[email protected]>
| Subject: Re: Does C# support array of classes?
| Date: Sun, 24 Aug 2003 02:37:38 +0100
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: m55-mp1.cvx3-a.bre.dial.ntli.net 62.255.96.55
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:178913
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Dear Andrea,
|
| I don't know about C++ but C# has Types with which you can do loads of
| classy stuff (pun intended).
|
| See my post in the thread "opening form by name" in which you can see
| how knowing the name of a form (which is also its class name) is enough to
| create and display an instance of the form using Types (and Reflection).
|
| With an array of Types you can do similar things and more for any
| class/type.
|
| Hope this piques your curiousity to study Reflection - it's very
| powerful.
|
| Best wishes,
| Fergus
|
|
|
 
Hi Zhanyong,

Reckon you're right there. :-)

And, on reflection (no pun this time), I'm aiming a bit further
into her future with .NET!

Regards,
Fergus
 
Back
Top