How to allocate memory in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
I want to know how to allocate memory in c#? Or, maybe I don't need to do
it. I'm quite new to c#, so I don't know many things.

Now, this is what I have in c#:

class A
{
private B[] MyArray;
private static int NumberofRecords = 0;
private int ArraySize = 10;

public A()
{
MyArray = new B[ArraySize];
}

public void AddRecord(string str)
{
MyArray [NumberofRecords] = new B(str);
NumberofRecords++;
if (NumberofRecords == ArraySize - 1)
{
ArraySize += 10;
RecordsArray = new B[ArraySize];
}
}
}

To clarify, the method AddRecord is the only place where the array grows
bigger. Class B has a constructor receiving a string.

I guess my code is wrong here, anyhow this is the functionality that I
want.(being able to let the array grow bigger, define exactly how much
bigger, and not to "lose" what I had before).

Please help,
Thank you very much.
 
Stop working so hard. Use a collection!

Don't use arrays of things, use a generic collection such as List<B>.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
And to add to Bob's reply, .Net is managed technology, and one aspect of it
is that it allocates and de-allocates memory by itself, or, in other words,
"manages" the memory. Aside from some classes that interact with unmanaged
DLLs, you should never have to worry about allocating or de-allocating
memory. For classes that interact with unmanaged DLLs, the IDisposable
interface is implemented, so that you can call the Dispose method of such
classes to free up any unmanaged memory.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Bob Powell said:
Stop working so hard. Use a collection!

Don't use arrays of things, use a generic collection such as List<B>.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.




Hi all,
I want to know how to allocate memory in c#? Or, maybe I don't need to do
it. I'm quite new to c#, so I don't know many things.

Now, this is what I have in c#:

class A
{
private B[] MyArray;
private static int NumberofRecords = 0;
private int ArraySize = 10;
public A()
{
MyArray = new B[ArraySize];
}

public void AddRecord(string str)
{
MyArray [NumberofRecords] = new B(str);
NumberofRecords++;
if (NumberofRecords == ArraySize - 1)
{
ArraySize += 10;
RecordsArray = new B[ArraySize];
}
}
}

To clarify, the method AddRecord is the only place where the array grows
bigger. Class B has a constructor receiving a string.

I guess my code is wrong here, anyhow this is the functionality that I
want.(being able to let the array grow bigger, define exactly how much
bigger, and not to "lose" what I had before).

Please help,
Thank you very much.
 
Hi all,
I want to know how to allocate memory in c#? Or, maybe I don't need to do
it. I'm quite new to c#, so I don't know many things.

Now, this is what I have in c#:

class A
{
private B[] MyArray;
private static int NumberofRecords = 0;
private int ArraySize = 10;

public A()
{
MyArray = new B[ArraySize];
}

public void AddRecord(string str)
{
MyArray [NumberofRecords] = new B(str);
NumberofRecords++;
if (NumberofRecords == ArraySize - 1)
{
ArraySize += 10;
RecordsArray = new B[ArraySize];
}
}
}

To clarify, the method AddRecord is the only place where the array grows
bigger. Class B has a constructor receiving a string.

I guess my code is wrong here, anyhow this is the functionality that I
want.(being able to let the array grow bigger, define exactly how much
bigger, and not to "lose" what I had before).

Please help,
Thank you very much.

Just to add a little bit to what the other guys have said. In terms of
the way that you think about objects and coding in C#, it is much
closer to Java than C++. Infact, I think C# was somewhat of a poor
name for the language.

As Kevin said, memory is already managed for you. When you create a
new object, the memory for it gets allocated for you. When the object
is no longer used, the memory for that object is automatically de-
allocted through the use of a Garbage Collector. Unlike C++, you don't
need to make any explicit "delete" calls to reclaim memory, and memory
leaks are far more difficult to cause (but not impossible).

In terms of your sample code that you provided, take a look at the
classes in the System.Collections.Generics library. These provide you
with templated classes to hold collections of objects that are type-
safe so that you get compile-time class-checking, and don't have to
make explict castings. The library also has several dynamically
growing collections so that you don't need to code up your own "grow"
methods.
 
Thanks a lot to all of you who have answered my question.
Thank you very much.

Evan M. said:
Hi all,
I want to know how to allocate memory in c#? Or, maybe I don't need to do
it. I'm quite new to c#, so I don't know many things.

Now, this is what I have in c#:

class A
{
private B[] MyArray;
private static int NumberofRecords = 0;
private int ArraySize = 10;

public A()
{
MyArray = new B[ArraySize];
}

public void AddRecord(string str)
{
MyArray [NumberofRecords] = new B(str);
NumberofRecords++;
if (NumberofRecords == ArraySize - 1)
{
ArraySize += 10;
RecordsArray = new B[ArraySize];
}
}
}

To clarify, the method AddRecord is the only place where the array grows
bigger. Class B has a constructor receiving a string.

I guess my code is wrong here, anyhow this is the functionality that I
want.(being able to let the array grow bigger, define exactly how much
bigger, and not to "lose" what I had before).

Please help,
Thank you very much.

Just to add a little bit to what the other guys have said. In terms of
the way that you think about objects and coding in C#, it is much
closer to Java than C++. Infact, I think C# was somewhat of a poor
name for the language.

As Kevin said, memory is already managed for you. When you create a
new object, the memory for it gets allocated for you. When the object
is no longer used, the memory for that object is automatically de-
allocted through the use of a Garbage Collector. Unlike C++, you don't
need to make any explicit "delete" calls to reclaim memory, and memory
leaks are far more difficult to cause (but not impossible).

In terms of your sample code that you provided, take a look at the
classes in the System.Collections.Generics library. These provide you
with templated classes to hold collections of objects that are type-
safe so that you get compile-time class-checking, and don't have to
make explict castings. The library also has several dynamically
growing collections so that you don't need to code up your own "grow"
methods.
 
Back
Top