3

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!

7
  • 1
    you made my day :) "The code is really ugly and I'm already getting confused looking at it" is something my boss says every time before he shows me his approach to a computational problem :) Commented Jan 20, 2012 at 16:53
  • Any type declared with class is a reference type, any variables/fields/... of that type will be references already. Many such references can all reference the same object. Thus, outside of interop, pointers are not needed. Commented Jan 20, 2012 at 16:54
  • 2
    It sounds like you really need a book. I'm not entirely sure what your question is -- what do you think you need a pointer for? Commented Jan 20, 2012 at 16:55
  • 1
    You may find this FAQ helpful: andymcm.com/csharpfaq.htm Commented Jan 20, 2012 at 16:55
  • C# uses references for non-value types automagically. And if you want to pass a reference to a function, use the ref keyword Commented Jan 20, 2012 at 16:56

7 Answers 7

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

4 Comments

Why the '(almost)' ? I can't think of any situation where they're not.
Passing reference types byval- still passing a pointer, but the pointer itself can be changed without affecting the caller, which might mess with someone who isn't familiar with .net references.
That always happens, your list1.Add(a) passes the reference by value but the object by reference.
Right, sorry, meant by ref of course. Multitasking doesn't make make for good SO commentary :-)
2
 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

3 Comments

Ah! This is interesting... So in C++ the "=" assignment would be "shallow" copy which wouldn't work in my case as my object has pointers to other objects, so I'd have to do a "Deep Copy" to make sure a2 really is a "copy" of a1. How about C#? could you do a "shallow" AND a "deep copy" or does copying a class only do "deep copy"?
@uberspaceguru In C# for deep copy you have to write your Clone method
@uberspaceguru In .NET/C#, there is no copying of the object going on if A is a class, only the reference is copied. If A is a class, then the above C# code is most similar to A *a1; A *a2 = a1; in C++. For .NET structures, the above code has similar meanings in both C# and C++ (a shallow copy).
1

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.

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.