简体   繁体   中英

Pointer alternative in C# for a C++ Programmer

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!

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.

Reference types in C# are (almost) always passed as pointers. If I do:

var a = new Object();
var list1 = new List<Object>();
var list2 = new List<Object>();
list1.Add(a);
list2.Add(a);

I am not duplicating a. I'm just putting a reference to it in both lists. Hope that helps.

 A a1;
 A a2 = a1;

In C++ the above code will make a1 and a2 two separate objects that are copies of each other. In C# if A is declared as a class a1 and a2 will refer to one and same object so you don't pointers per se for what you are trying to acheive

Instance of classes in C# are references and thus are analogous to pointers. In C# when you write a = b where a and b are instances of classes, the only thing that is copied is the reference to the object. You now have two references to the same object.

As I read your question, C# references will serve your purposes.

Arraylist would work fine in your situation. Objects passed to the arraylist are just references (similar to pointers). They are not recreated, so there won't be any duplicates.

You don't have to cope with pointers as C# passes references for object types.

And you should have a look at the HashSet / generic Lists which C# provides as collections.

Another way to solve pointer issue inC#. WeakReference . http://msdn.microsoft.com/en-us/library/ms404247.aspx

C# "shared pointer" for alternative memory management?

Example:

// Create the object
Book book = new Book("My first book", "Me");
// Set weak reference
WeakReference wr = new WeakReference(book);
// Remove any reference to the book by making it null
book = null;
if (wr.IsAlive)
{
    Console.WriteLine("Book is alive");\
    Book book2 = wr.Target as Book;
    Console.WriteLine(book2.Title);
    book2 = null;
}
else
    Console.WriteLine("Book is dead");

// Lets see what happens after GC
GC.Collect();

// Should not be alive
if (wr.IsAlive)
    Console.WriteLine("Book is alive");
else
    Console.WriteLine("Book is dead");

The output should be

Book is alive
My first book
Book is dead

http://www.switchonthecode.com/tutorials/csharp-tutorial-weak-references

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM