Problem Description
using System;
using System.Linq;
public static class Program {
public static int[] Puzzle(int[] a) {
return a.OrderBy(s => s).ToArray();
}
}
My code and wrong information
Bad Dependency
[System.Core]System.Linq.IOrderedEnumerable`1
Why I am wrong?Whether or not the platform can support OrderBy?Do I need a custom sort function?The code can run successful in VS2015, I have tried.
www.codehunt.com
Try follow.
using System;
using System.Linq;
public static class Program {
public static int[] Puzzle(int[] a)
{
return Array.Sort(a);
}
}
Related
I have this class library I created. And I am running into the issue of getting the library to work with a console application. This is just a basic assignment I'm working on but I do not know where I am going wrong. Here are my files:
Class Library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2
{
public class Test
{
public static string GetData(String prompt)
{
Console.WriteLine(prompt);
return Console.ReadLine();
}
public static int GetInt()
{
return Convert.ToInt32(GetData("Enter an interger"));
}
public static void put(String output)
{
Console.WriteLine(output);
}
public static void put(String output, String heading)
{
put(heading + '\n' + output);
}
public static void Main(String[] args)
{
int x = GetInt();
int y = GetInt();
if (x > 0 && y > 0) put(String.Format("Sum:{0}", x + y));
}
}
}
Console Application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test2;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test myTest = new Test();
Test.GetData(prompt);
}
}
}
My main issue I guess I have is calling the methods to run in the console application. No matter how much reading I do I can't seem to know what I am doing wrong.
You don't need a Main method in your class library. The Main method in your console application is the entry point to your application.
If the functionality in the Main method you've defined in your class library is what you're trying to achieve for the overall application then consider moving that functionality into the Main method in your console application.
You've made the methods in your class library static so you don't need to instantiate an object of type Test
So my solution for you would be to have this in your Main method of your console application:
int x = Test.GetInt();
int y = Test.GetInt();
if (x > 0 && y > 0) Test.put(String.Format("Sum:{0}", x + y));
Console.Read();
I would suggest reading up on Static Classes and Static Class members here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members
When deciding to make a Static class I always come back to this quote from the above link:
A static class can be used as a convenient container for sets of
methods that just operate on input parameters and do not have to get
or set any internal instance fields.
I would also consider changing the name of your Test class. Even in small test applications like this I find it is useful to use well defined names with meaning. It's good practice so that you will do it automatically in larger applications.
Finally, good luck with all your learning!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class test
{
private double job = 4.2; // <-- declared it here
job = 5.7; // Giving me an "Error CS0103 The name 'job' does not exist in the current context."
}
class Program
{
static void Main(string[] args)
{
}
}
}
The double variable "job" or any other variable that I create (ex: public or static) I cannot use in the class. This is happening in Visual Studio 2015. I have not seen this before nor do I know what could cause this so any help would be appreciated.
You can't change the variable inside the class itself. You're only allowed to change it inside a function:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class test
{
private double job = 4.2; // <-- declared it here
void changeJob() {
job = 5.7; // Changed the line to be inside a function
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
Right inside the 'Test' class is declaration level. 'job=5.7' is not declaration. You can always use it inside any method, but not at declaration level.
simple code here and the answers I find don't seem to work.
I'm using
SharpDevelop Version : 3.2.1.6466
.NET Version : 2.0.50727.5485
The problem is the error code
Expected class, delegate, enum, interface, or struct (CS1518).
Any ideas?
Program.cs codes:
using System;
using System.Threading;
namespace Threshold
{
public class Class1
{
public Class1()
{
Heritage YOLO = new Heritage();
YOLO.Fractal();
}
}
static void Main()
{
//do nothing
}
}
The cs file it calls is:
using System;
using System.Threading;
namespace Threshold
{
public class Heritage
{
int Fractal()
{
//Do stuff.
}
}
internal partial class DefineConstants
{
public const string DRIVERPATH = "d:\\tc\\bgi";
}
}
Please help with a fix.
Thanks.
Your main method is outside the class. Put it inside.
See this code:
namespace TestHtmlDecode
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
[TestClass]
public class TestHtmlDecode
{
private string Convert(string input)
{
return HttpUtility.HtmlDecode(input);
}
[TestMethod]
public void TestLeftBrace()
{
Assert.AreEqual("{", Convert("{"));
}
[TestMethod]
public void TestGreaterThan()
{
Assert.AreEqual(">", Convert(">"));
}
}
}
TestGreaterThan passes, but TestLeftBrace fails (Convert returns {). Why is this?
Looks like there are two things going on here.
&lbrace is a { and not [ (http://jsfiddle.net/B7AAh/1/)
It doesn't look like &lbrace is included in the list of of known items. Source code is here http://referencesource.microsoft.com/#System/net/System/Net/WebUtility.cs which refers to the list of entities found here http://www.w3.org/TR/REC-html40/sgml/entities.html
I'm attempting to extend my custom classes and running into a problem where it cannot find the extension method.. I have and can extend any built in classes or even ones contained within DLL's. I don't know if this is a compilation error or if I'm doing something wrong. Threw together a small program for an example, won't compile..
Here's the extension:
namespace ExtensionMethodTesting.Extension
{
public static class Extension
{
public static void DoSomething(this ExtensionMethodTesting.Blah.CustomClass r)
{
}
}
}
Here's the Custom Class:
namespace ExtensionMethodTesting.Blah
{
public class CustomClass
{
public static void DoNothing()
{
}
}
}
Here's the code calling it:
using ExtensionMethodTesting.Blah;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtensionMethodTesting.Extension;
namespace ExtensionMethodTesting
{
class Program
{
static void Main(string[] args)
{
CustomClass.DoNothing();
CustomClass.DoSomething();
}
}
}
I must be missing something... Anyways the exact error just for clarification is:
Error 1 'ExtensionMethodTesting.Blah.CustomClass' does not contain a definition for 'DoSomething' c:\users\damon\documents\visual studio 2013\Projects\ExtensionMethodTesting\ExtensionMethodTesting\Program.cs 16 25 ExtensionMethodTesting
Extension methods require an instance of an object. You'll have to new up a CustomClass to use it.
var custom = new CustomClass();
custom.DoSomething();
See this answer as to why that is.
You need to instantiate an object of the CustomClass to use its extension method.
CustomClass obj = new CustomClass();
obj.DoSomething();