All Questions
3,849
questions
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 ...
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 ...
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......
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 ...
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 ...
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?
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 ...
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 ...
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 ...
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 ...
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()))
{
...
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?...
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 ...
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 ...
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 ...