reference-type

What's the difference between struct and class in .Net?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...

Can you have a Class in a Struct

Is it possible in C# to have a Struct with a member variable which is a Class type? If so, where does the information get stored, on the Stack, the Heap, or both? ...

In C#, why is String a reference type that behaves like a value type?

A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference the same object. Why isn't string just a value type then? ...

C# How to find the size of a reference type

Hi guys, I was wondering if there is a way to find the size of a reference type in C#. I've done some googling and the general idea on the forums seem to be that this isn't possible. I thought I'd ask you guys and see if anyone here knew better ;) ([cough] Jon Skeet [cough]) After all, profiling tools must have a way of doing this? ...

What is the fastest way to find if an array of byte arrays contains another byte array?

I have some code that is really slow. I knew it would be and now it is. Basically, I am reading files from a bunch of directories. The file names change but the data does not. To determine if I have read the file, I am hashing it's bytes and comparing that to a list of hashes of already processed files. There are about 1000 files in each...

get attribute of a property in .NET after passing the property to a function as ref

is it possible to pass a property eg. Person.Firstname by reference to a function and then still be able to read the attributes of Person.Firstname through this reference type? so does the reference type know that it is not only a string but also the Firstname property of class Person? tia i try to write a extension for my asp.net m...

Nullable<> as TModel for ViewPage

What are the possible reasons what Nullable<> types are disallowed to be passed as TModel parameter of System.Web.Mvc.ViewPage<TModel> generic? This could be handy sometimes. In ASP.NET MVC source defined what TModel should be a class: public class ViewPage<TModel> : ViewPage where TModel : class but Nullable types are value types. M...

Use cases for boxing a value type in C#?

There are cases when an instance of a value type needs to be treated as an instance of a reference type. For situations like this, a value type instance can be converted into a reference type instance through a process called boxing. When a value type instance is boxed, storage is allocated on the heap and the instan...

Mutable wrapper of value types to pass into iterators

I'm writing an iterator that needs to pass around a mutable integer. public IEnumerable<T> Foo(ref int valueThatMeansSomething) { // Stuff yield return ...; } This nets me "Error 476 Iterators cannot have ref or out parameters". What I need is this integer value to be modified in the iterator and usable by the caller of the...

C# supports value types and reference types, but are they all objects?

Hi, I know C# has both value and reference types, but how can you do a this: int age = 100; string blah = age.ToString(); If age is a value type, how does it have a ToString method on it? Does it get converted to an object ONLY when required internally then? ...

How do I pass reference types between webservices?

I'm having a bit of difficulty passing a reference type between webservices. My set up is as follows. I have a console application that references two web-services: WebServiceOne WebServiceTwo WebServiceOne declares the details of a class I am using in my console application...let's call it MyClass. My console application calls We...

Cloning a C# Reference Type to a Derived Reference Type

Coming from a C++ background, I am finding cloning of objects in C# a little hard to get used to. To clear up some of my confusion, I am looking for an elegant way to clone an object of a base type to a derived type. To illustrate: public class Base { public string Member1; public int Member2; public float Member3; pub...

Implementing Nullable Types in Generic Interface

So in a previous question I asked about implementing a generic interface with a public class and bingo, it works. However, one of the types I'm looking to pass in is one of the built in nullable types such as: int, Guid, String, etc. Here's my Interface: public interface IOurTemplate<T, U> where T : class where U : class { ...

What's the size of a reference on the CLR

I was (purely out of curiosity) trying to find out what the size of an actual reference is when an allocation is made on the stack. After reading this I still don't know (this answers it only for value types or type definitions), and I still cannot seem to find it anywhere. So basically imagine a class as follows class A { string ...

Performance of Sorting Reference Type vs Value Types

We were trying to sort a collection of FileInfo objects in .NET. We implemented our IComparer to ensure that FileInfo objects were sorted based on our criteria. We then noticed that performance of sorting the FileInfo objects was many times slower than just ints. On a hunch (and remembering how references work in C) we were able to im...

Teaching References in C#

In a couple of weeks, I'll be teaching a class of first-year engineers the salient points of references in C# as part of their first-year programming course. Most of them have never programmed before, and had enough trouble learning objects, so teaching references is going to be an uphill battle. I plan to have lots of examples availab...

typeof(System.Enum).IsClass == false

Founded that: typeof(System.Enum).IsClass == false It's become strange that System.Enum has also .IsValueType == false, but Reflector shows that it is really just an abstract class. System.Enum is a reference type like a System.ValueType and casting enumeration values to/from System.Enum reference caused boxing/unboxing. No surprises...

Storing a reference in c#

I'm trying to design a class which can update a reference to an object (outside the class) on destruction. So essentially you create an instance of this object and pass it a reference type (in whichever manner, constructor etc.) and then on destruction of the object the original reference has changed to a reference created by the obje...

C++/CLI: Native Reference vs Tracking Reference

What is the difference between the following two functions? ref class SomeClass; void swap(SomeClass^& a, SomeClass^& b){ SomeClass^ c = a; a = b; b = c; } void swap2(SomeClass^% a, SomeClass^% b){ SomeClass^ c = a; a = b; b = c; } ...

Reference question: when are two objects equal?

I have a Vector class, and I was testing the following unit test (using nUnit). 1 Vector test1 = new Vector(new double[] { 6, 3, 4, 5 }); 2 Vector test2 = test1; 3 Assert.AreEqual(test1, test2, "Reference test"); 4 test2 = new Vector(new double[] { 3, 3, 4, 5 }); 5 Assert.AreEqual(test1, test2, "Reference test"); The first test i...