Browsing 7239 questions and answers with Jon Skeet

Creating a method with generic param

I am trying to create a generic method in a static class like below to which I will pass paramData at run time. public static void InsertBulk<T>(string procedure,...
Jon Skeet
people
quotationmark

There are two separate things to understand here: How you declare a generic method How you call a generic method You need to include the <T> in the method declaration to make it a generic method. Without that, the compiler... more 7/6/2017 6:37:44 AM

people

How to skip first few elements from array?

I have an array with total 5000 elements and at one functionality, I only need last 3000 elements only to proceed further. for that I have tried following solution. //skipping...
Jon Skeet
people
quotationmark

If you absolutely have to use an array, then Array.Copy is probably your friend: int[] smallerArray = new int[array.Length - 2000]; Array.Copy(array, 2000, smallerArray, 0, smallerArray.Length); I'd expect that to be a bit more... more 7/5/2017 2:16:36 PM

people

Compare two objects by reference when equality operator is overridden

I need to check if two objects of the same type are the same instances and point to the same allocation of memory. The problem is that the type has overloaded equality operator...
Jon Skeet
people
quotationmark

Operators can't be overridden - they can only be overloaded. So the == operator in object.ReferenceEquals is still comparing references, or you could do the same thing yourself by casting one or both operands: string x = "some... more 7/5/2017 11:28:33 AM

people

C# Attach properties? to enum

I have an enum: enum RANK { kingdom=0, phylum=1, order=2, family=3, genus=4, species=5 } where each item has a parent (the rank above) and child (the...
Jon Skeet
people
quotationmark

No, there's no such thing as an extension property. But you could create your own class: public sealed class Rank { private static readonly List<Rank> ranks = new List<Rank>(); // Important: these must be initialized... more 7/5/2017 10:12:07 AM

people

'An item with the same key has already been added.' Which key?

The following code: var rnd = new Random(); Enumerable.Range(1,20).Select(x => rnd.Next(10)).ToDictionary(x => x, x => ""); will inevitably fail with the following...
Jon Skeet
people
quotationmark

I don't believe it's feasible to get that information from the exception with the current desktop .NET framework. (Although you can do it in the debugger, if you're willing to jump through some hoops.) The good news is that in .NET Core,... more 7/5/2017 6:08:35 AM

people

Double quotes in c# doesn't allow multiline

e.g. string str = "{\"aps\":{\"alert\":\"" + title + "" + message + "\"}}"; I need to make it as for readability: string str = " { \"aps\": { ...
Jon Skeet
people
quotationmark

If you really need to do this in a string literal, I'd use a verbatim string literal (the @ prefix). In verbatim string literals you need to use "" to represent a double quote. I'd suggest using interpolated string literals too, to make... more 7/5/2017 5:51:57 AM

people

c# DataSet.WriteXml(fileStream, XmlWriteMode.IgnoreSchema); corrupts my xml file

<Data> <configs> <someEntry>"value1"</someEntry> </configs> <profiles> <someEntry>"value2"</someEntry> ...
Jon Skeet
people
quotationmark

You're using FileMode.Open, which doesn't truncate the file if it already exists. Just use File.Create instead: using (var stream = File.Create(directory)) { ... } (You could specify FileMode.Create instead, as your post originally... more 7/4/2017 7:39:01 PM

people

Replacing byte in file

I'm trying to replace certain bytes from a file with some other specific bytes, but have a problem with my binary writer replacing too many bytes. What is wrong in my code? using...
Jon Skeet
people
quotationmark

Basically you don't want or need to use BinaryWriter for this. You're calling BinaryWriter.Write(int) which is behaving exactly as documented. Just use FileStream to write a single byte: using (var stream = File.Open(fileName)) { ... more 7/4/2017 11:01:55 AM

people

Get List<Guid> from object C#

I have function GetChildren, which returns all the child nodes, in a structure as they need to be in treeview. public static object GetChildrenNodes(IEnumerable<NodeDto>...
Jon Skeet
people
quotationmark

I would avoid using the same method. You've got two different requirements: Build a collection of anonymous types for use in the UI Build a list of GUIDs for children of the given node ID (I'm assuming you only want direct children,... more 7/4/2017 8:44:11 AM

people

How to access constants from a java class through other variable?

I have java class which has all constants as given below: public final class CategoryIDs { public static final String Extraction_of_natural_gas = "1111"; public static final...
Jon Skeet
people
quotationmark

If you really, really need to do this with your current structure, you could use reflection. However, you may well find that an enum would be a better fit. Something like: public enum CategoryID { ... more 7/4/2017 8:27:52 AM

people