Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Prozak

C# & Classes

This topic is 5310 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

Hi all, im a complete n00b in C#, although i code rather well in C++. I created a small class in C#, but couldnt use it like: myclass abc; i had to do something like: myclass abc = new myclass; then i could use it. is this standard practice? if im responsible for creating the class instance, do i also have to kill it? thanx in advance,

[Hugo Ferreira][Positronic Dreams]
All your code are belong to us!

Share this post


Link to post
Share on other sites
Advertisement
Hey,

C# does not allow you to use variable''s, etc with out initializing them. It is perfectly normal.

C# has what''s called Garbage Collection. It will take care of any memory that you are no longer using. So, if you made a new instance and then set it equal to null, the GC would see that you are done with the old memory and mark it as finalizeable.

I could be wrong, but that is my understanding. I''ve only recently gone to C# from C++ as well Good Luck!

Michael Bartman
CEO, Lead Programmer
Dark Omen Studios

Share this post


Link to post
Share on other sites
quote:
I could be wrong, but that is my understanding


nah, you''re not wrong.

C# handles memory for you internally with a garbage collecter. You dont need to be deleting anything


:::: [ Triple Buffer ] ::::

Share this post


Link to post
Share on other sites
I think the real reason why you cant do what you did is that Classes in C# have to be created on the heap. Thus
MyClass newclass = new MyClass();

However, structs in C# are allocated on the stack so (without testing myself) you may be able to do

MyStruct struc;

as I''m fairly certain you can do

int i;

etc.. without initialising the variable. though you will get a warning or possiblly and error if you dont initialise and try to use it.

Share this post


Link to post
Share on other sites
You can use structs in that manner, but none of the variables within the struct will be initialized to a default if I remember correctly. I do know that there is a catch, and I am pretty positive that is it.

Michael Bartman
CEO, Lead Programmer
Dark Omen Studios


[edited by - MichaelBartman on September 22, 2003 4:33:27 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by pentium3id
Hi all,
im a complete n00b in C#, although i code rather well in C++.
I created a small class in C#, but couldnt use it like:

myclass abc;


That line in C# is pretty much equivalent to myclass* abs; in C++. You only declared a pointer to the object - you didn''t initialize the pointer to anything.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There are two types in C#, value types and reference types. Reference types must be created on the heap (using the new keyword). An object is a reference types.

The only time you need to do memory clean up is on objects that inherit from IDisposable and expose the Dispose method. In which case, you should call the Dispose method when you are finished with the object. Of course, you can even have .NET handle that for you as well by using the using statement;

using(object) {
...
}

This will automatically call Dispose (if applicable) on the object when the end of the brackets are reached.

Share this post


Link to post
Share on other sites
Using C++ DLLs in C# is actually really easy.

You just do something like:

[DllImport("my.dll")]
public void DllFun();

Just make sure you declare the functions in the C++ DLL w/ C so it gets rid of name decoration.

Share this post


Link to post
Share on other sites

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