Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer alternative in C# for a C++ Programmer

Tags:

c++

c#

transition

I'm primarily a C++ coder and haven't touched c# in a few years (so, forgive me if I'm asking a question which may be a brain fart in my part).

Background info: I'm writing a file organizing utility (just for fun, and to help clean up duplicates on my computer). I've been able to do MD5 checksums for files and files and find duplicate files in various sub directories, sometimes with the same file name and sometimes not (always the same file type though). I initially did this by using just using the file path strings and Winform objects, Arraylist, Arrays in the code behind for the "MainFrom", as a proof of concept. The code is really ugly and I'm already getting confused looking at it, so it's definitely not maintainable.

So I figured a more elegant and sensible design would be to store as an object which would have simple things like filepath, filename, MD5, fileType etc etc. (yes, I know about FILE). Then I figured it would make sense if it had references to other instances of objects which were "duplicates", "similar". I'm looking to create an array of pointers which is part of the object which would point to duplicate objects. That way, at runtime I could go thru all duplicates, figure out their properties etc.

I was planning on later inheriting from this class for specific file types such as mp3 or jpg files where I may be able to compare content (ex: I could identify which pictures may simply be resized versions of one another and have them point to each other). But C# doesn't have pointers. I was looking at delegates, but then again, that's not really what I want.

My Dilemma: C# doesn't have pointer in the managed code (I don't want to use unmanaged sections unless I absolutely have to).

I've also thought about creating something like an arraylist and passing in "objects" at runtime. But doesn't that create duplicates? it's not really a reference to the new object is it?

I would really appreciate advice from those who've made the C++ to C# transition as to how I move beyond this. Please feel free to let me know if I'm totally approaching the design wrong here. (I'm assuming since they're both object oriented, such a design would work in both worlds).

I would really appreciate references to other sources which can help (since I'm not the first c++ coder trying to code in C#). Thanks in advance!

like image 352
uberspaceguru Avatar asked Dec 06 '22 16:12

uberspaceguru


1 Answers

My Dilemma: C# doesn't have pointer in the managed code

But it uses references for objects (including arrays) everywhere.

var a = new StringBuilder();
var b = a;      // a and b now refer to the same single StringBuilder instance
a.Append("!");  // equivalent to a->Append("!"); in C++

So what functionality are you actually missing that you think (only) pointers can solve?

creating something like an arraylist and passing in "objects" at runtime. But doesn't that create duplicates? it's not really a reference to the new object is it?

No, an ArrayList can only store references to objects so it generally does not require copying or cloning. That only happens for Value Types (int, double). But be sure to use List<MyClass> instead, for more functionality and less type-casting.

In short, read up on Reference-Types, Value-Types and references before you proceed.

like image 136
Henk Holterman Avatar answered Dec 21 '22 22:12

Henk Holterman