Browsing 15589 questions and answers with Jon Skeet

C# Trouble Setting Variable

So I'm trying to save the object clicked on by the mouse to a seperate variable, but the RaycastHit isn't converting to GameObject, even in an if statement checking its...
Jon Skeet
people
quotationmark

Just using the is operator doesn't change the compile-time type of the hit variable. You could either cast within your if body, or use as instead: var hitGameObject = hit as GameObject if (hitGameObject != null) { selectedObject =... more 3/6/2017 10:24:40 PM

people

How to pass DateTime.Ticks as URI parameter?

How can I get this code working? public ActionResult ABC(DateTime.Now.Ticks Id) { //some stuff return View(); } Your thoughts? EDIT The whole concept is have unique URLs for...
Jon Skeet
people
quotationmark

First, the basics: when declaring a method, you need to specify the type of the parameter, which in this case would be long: public ActionResult Abc(long id) { // ... } When you call that (whether internally or whether it's... more 3/5/2017 12:35:34 PM

people

My delegate type isn't working "Method name expected"

I tried this: class Program { public delegate int add(int x, int y); public class ff { public static int addNumbers(int x, int y) { return...
Jon Skeet
people
quotationmark

The type of your delegare variable is just Delegate. That could refer to any delegate. In order to invoke a delegate (in the normal way), you should have an expression of the appropriate type. After fixing the naming conventions and... more 3/4/2017 4:11:52 PM

people

Convert String With Zeros To Decimal

i have a string like this: "000123". I want to know how to convert this string to decimal but keep the zero numbers. I have used Convert.ToDecimal(),Decimal.TryParse &...
Jon Skeet
people
quotationmark

No, it's not. System.Decimal maintains trailing zeroes (since .NET 1.1) but not leading zeroes. So this works: decimal d1 = 1.00m; Console.WriteLine(d1); // 1.00 decimal d2 = 1.000m; Console.WriteLine(d2); // 1.000 ... but your leading... more 3/3/2017 7:26:30 AM

people

How do i check if a particular string is not present in a list in c#?

I have a list as follows, List<string> variablelist = new List<string> { "first", "second","third"}; And i have one more list, like List<string> method =...
Jon Skeet
people
quotationmark

LINQ is the simplest way of doing this, with the Except method: var inOnlyVariableList = variableList.Except(method).ToList(); The result will be a List<string> of strings which are in variableList but not in method. more 3/2/2017 11:51:41 AM

people

Is Java's import keyword for source files or binary files?

I know I can include a class or collection of classes in my Java project using the import statement. For example, import java.io.utils.* imports (i.e. makes available for use in...
Jon Skeet
people
quotationmark

Import just means "make the imported classes available by their simple names" - you can remove imports entirely if you use fully-qualified names everywhere. It's definitely not like #include in C for example. When you compile, if you try... more 3/2/2017 7:47:12 AM

people

C# List Order by which row have max value then order by ascending

So I have a List<Tuple<string, integer>> A 10 A 11 B 12 B 13 And I want the following as a result B 12 B 13 A 10 A 11 Basically I want to check...
Jon Skeet
people
quotationmark

It sounds like you just need: var ordered = list.OrderByDescending(tuple => tuple.Item1) .ThenBy(tuple => tuple.Item2); The "then-by" ordering criterion is only used for items which have equal values under the... more 3/2/2017 7:35:51 AM

people

List<PropertyInfo> except List<PropertyInfo> not working

I'm making a helper method where it would automatically set random values to a property of a given entity (class) so that I won't have to fill each property with a value when...
Jon Skeet
people
quotationmark

PropertyInfo "knows" which type you used to ask for it. For example: using System; using System.Reflection; class Base { public int Foo { get; set; } } class Child : Base { } class Test { static void Main() { ... more 3/1/2017 3:00:40 PM

people

narrow xdocument in c#

Is there any way to to cut off some part of xDocument and still getting xDocument? By cutting off I mean keeping selected node. I have XML like this: <something 1> ...
Jon Skeet
people
quotationmark

You mean you want only the first child node? Yes, that's simple using the Remove extension method: doc.Root.Elements().Skip(1).Remove(); Sample: using System; using System.Linq; using System.Xml.Linq; class Test { static void... more 3/1/2017 9:35:52 AM

people

Int array as a key in a Dictionary VS a string

I have a dictionary right now that is using a string as the Key, and a GameObject as the value. The strings are all IP address's, so they are not very long. Here is an...
Jon Skeet
people
quotationmark

As ever, you should test performance rather than guessing about it or asking others to guess. However, two things to think about: You've shown an IPv4 address... what about IPv6? That could definitely complicate things The IPv4 address... more 2/28/2017 3:58:48 PM

people