Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
358 votes
9 answers
502k views

How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]

I have a list of objects that have two int properties. The list is the output of another linq query. The object: public class DimensionPair { public int Height { get; set; } public int ...
theringostarrs's user avatar
295 votes
14 answers
583k views

Inconsistent Accessibility: Parameter type is less accessible than method

I'm trying to pass an object (a reference to the currently logged on user, basically) between two forms. At the moment, I have something along these lines in the login form: private ACTInterface ...
dodgrile's user avatar
  • 3,159
248 votes
9 answers
320k views

C#: Printing all properties of an object [duplicate]

Is there a method built into .NET that can write all the properties and such of an object to the console? One could make use of reflection of course, but I'm curious if this already exists......
Svish's user avatar
  • 156k
237 votes
4 answers
858k views

Creating a copy of an object in C# [duplicate]

Please have a look at the code below (excerpt from a C# book): public class MyClass { public int val; } public struct myStruct { public int val; } public class Program { private static ...
afaolek's user avatar
  • 8,717
194 votes
16 answers
207k views

What is the best way to dump entire objects to a log in C#?

So for viewing a current object's state at runtime, I really like what the Visual Studio Immediate window gives me. Just doing a simple ? objectname Will give me a nicely formatted 'dump' of the ...
Dan Esparza's user avatar
  • 28.3k
174 votes
11 answers
531k views

How to cast Object to its actual type?

If I have: void MyMethod(Object obj) { ... } How can I cast obj to what its actual type is?
Paul Lassiter's user avatar
162 votes
13 answers
343k views

Convert any object to a byte[]

I am writing a prototype TCP connection and I am having some trouble homogenizing the data to be sent. At the moment, I am sending nothing but strings, but in the future we want to be able to send ...
Steve H.'s user avatar
  • 3,373
160 votes
5 answers
157k views

How to access property of anonymous type in C#?

I have this: List<object> nodes = new List<object>(); nodes.Add( new { Checked = false, depth = 1, id = "div_" + d.Id }); ... and I'm wondering if I can ...
wgpubs's user avatar
  • 8,211
135 votes
12 answers
146k views

How to do ToString for a possibly null object?

Is there a simple way of doing the following: String s = myObj == null ? "" : myObj.ToString(); I know I can do the following, but I really consider it as a hack: String s = "" + myObj; It would ...
codymanix's user avatar
  • 29.2k
111 votes
20 answers
241k views

Comparing object properties in c# [closed]

This is what I've come up with as a method on a class inherited by many of my other classes. The idea is that it allows the simple comparison between properties of Objects of the same Type. Now, this ...
nailitdown's user avatar
  • 7,928
90 votes
19 answers
253k views

How to convert object to Dictionary<TKey, TValue> in C#?

How do I convert a dynamic object to a Dictionary<TKey, TValue> in C# What can I do? public static void MyMethod(object obj) { if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { ...
Martin Braun's user avatar
86 votes
10 answers
97k views

Why doesn't Any() work on a c# null object

When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren't 'any', and it should probably return false. Why does C# behave this way?...
DefenestrationDay's user avatar
81 votes
4 answers
125k views

Checking if the object is of same type

Hello I need to know how to check if the object of the same type in C#. Scenario: class Base_Data{} class Person : Base_Data { } class Phone : Base_data { } class AnotherClass { public void ...
slao's user avatar
  • 2,036
79 votes
5 answers
51k views

What is the difference between a variable, object, and reference? [duplicate]

Exactly what are the differences between variables, objects, and references? For example: they all point to some type, and they must all hold values (unless of course you have the temporary null-able ...
user avatar
78 votes
6 answers
57k views

What does the keyword "new" do to a struct in C#?

In C#, Structs are managed in terms of values, and objects are in reference. From my understanding, when creating an instance of a class, the keyword new causes C# to use the class information to make ...
KMC's user avatar
  • 20k

15 30 50 per page
1
2 3 4 5
257