Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to understand usage of Main function in C#.
I am trying to write separate method (ex. Method1) to do the action such as:
Asking question to user (what is your name).. return some response ("Glad to meet you, XXX").
I want to put all the functionality such as Asking question and returning response on a separate method (Method1) instead of using Main.
Then, how can I pass step from Main (starting position) to go to that Method1?
I guess my question is how do I call Method1 from Main?
What kind of information do I have in Main method then?
Do I just put like this way?
static void Main(string[] args)
{
Method1();
Console.ReadKey();
}
The Main function in C# is what's known as the entry point of your program. If your program was a book and the computer wanted to start reading it, it has to begin from somewhere - that's where Main comes in. It's the method that gets called to get your program going.
As you might have noticed, main is a static method:
public static void Main(){
// Your code here
}
Without going into too much detail, a static method can only call other static methods, or create an instance of something. So if you wanted Main to call something else, the two options are like this:
public static void Main(){
Method1();
}
// Method1 is also static:
public static void Method1(){
Console.WriteLine("Hello!");
}
Or alternatively by creating an instance:
public class MyProgram{
public static void Main(){
// Create an instance of this class:
MyProgram program=new MyProgram();
// And call Method1 on the instance:
program.Method1();
}
// Notice how method1 is not static this time:
public void Method1(){
Console.WriteLine("Hello!");
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have method e.g.
public async Task<int> AddUser()
{
//some logic
await _externalClass.DoSomething();
//some logic
}
And I want to get value from method await _externalClass.DoSomething(); (return type is Task and return type cannot be changed by me)
To make code like that:
public async Task<int> AddUser()
{
//some logic
await _externalClass.DoSomething();
if(valueThatIWantToGetFromExternalMethod)
{
//some logic
}
//some logic
}
How can I make this value from external method (this should be bool)valueThatIWantToGetFromExternalMethod ?
I thought that I can use ref or out keyword, but we can't do this in async methods in C#. Can someone show me how to implement this kind of logic?
You can pass a reference object into the method. Then changes made to the object will be available for you inside the AddUser scope.
string or class is a reference type.
That is if you cant return a Task from your DoSomething method
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a class called Foo and a static method - Bar (Foo instance) - inside it. But instead of only being able to call it like Foo.Bar(new Foo()), I want to also be able to say new Foo().Bar();
TL;DR I have a static method in class Foo which has a parameter of type Foo and I want to also be able to call it as if it was a non-static method.
This is all for testing purposes. I know this'd be terrible code design.
Create both methods, and let one redirect to other, where main logic is:
public class Foo
{
// Called by Foo.Bar(new Foo());
public static void Bar(Foo instance)
{
// logic here
}
// Called by new Foo().Bar();
public void Bar()
{
Foo.Bar(this);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was looking for approaches that could be used to access a static class which contains private static methods to unit test.
I came across PrivateObject and PrivateType, I do not understand what the difference between them is, and how you would decided to use either one.
BusinessLogic
public static Class BarLogic(){
private static void FooBar(){
//do something here
}
}
API
public class FooService(){
BarLogic.FooBar();
}
The difference is whether you're accessing otherwise inaccessible instance members or static members.
In the example below, you would use PrivateObject to access _someInstanceField, and PrivateType to access _someStaticField.
public class SomeObjectWithInAccessibleMembers
{
private int _someInstanceField;
private static int _someStaticField;
}
Given that your test was in a separate assembly from this class (and therefore unable to to see its internal members) you could do this:
var privateObject = new PrivateObject(new SomeObjectWithInAccessibleMembers());
privateObject.SetField("_someInstanceField", 1);
Assert.AreEqual(1, privateObject.GetField("_someInstanceField"));
var privateType = new PrivateType(typeof(SomeObjectWithInAccessibleMembers));
privateType.SetStaticField("_someStaticField", 1);
Assert.AreEqual(1, privateType.GetStaticField("_someStaticField"));
I've never used this. I didn't even know about it. My first reaction is that it couples the test to the inner implementation of the class instead of testing its public methods. Then it couples it some more by using strings for member names, so the tests could break just by changing a member name.
On the other hand, I've worked in projects where a) I want to add unit tests, but b) I can't refactor to make it more testable. It's not my first choice but I might have a use for this. Thanks!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
How C#6 using static feature should be used in right way? It really looks nice for cases like shortening string.Format (CultureInfo.InvariantCulture, "Some format"); to Format (InvariantCulture, "Some format");, but is it always the case?
Take this for example. You have enum like this:
enum Enum { Value1, Value2 }
And you decide to to use it in code like this:
using static {Namespace}.Enum;
// ...
var value = Value1;
Latter on you decide to create a class named Value1. Then your code var value = Value1 will generate compile error:
'Value1' is a type, which is not valid in the given context
Or other case. You have two classes with different static methods:
class Foo {
public static void Method1() { }
}
class Foo2 {
public static void Method2() { }
}
And you use it in 3rd class like
using static {Namespace}.Foo;
using static {Namespace}.Foo2;
// ...
class Bar {
void Method() {
Method1();
Method2();
}
}
Which works fine. But if you decide to introduce Method2 in Foo class this code will generate compile error:
The call is ambiguous between the following methods or properties: 'Foo.Method2()' and 'Foo2.Method2()'
So my question is what is the right way of using using static feature?
The "problems" being stated were already present in previous versions of the language regarding type name resolution.
This feature just brings those "problems" down to type members.
I've not read any recommendations on the topic. But my opinion is that you can use using static for stuff that gives you names that makes sense on their own.
using static Math;
var max = Max(value1, value2);
In the case of string.Format I think it becomes unclear what Format means (all kinds of stuff can be formatted into anything that has a kind of format).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working with C#.NET and basically have a page containing many areas.
In my code-behind, I basically want to be able to do something like:
bool result1 = MyClass.Section["area1"].Process();
bool result4 = MyClass.Section["area4"].Process();
I need to write a class that would call some kind of "Process" method and be able to have it accept a parameter like "area1" inside that method.
Any help on getting me started with this would be greatly appreciated, thank you!
Following the normal .NET naming conventions I'll assume you mean, by your example, that MyClass is being referenced statically rather than by instance (which may not be a big change). Given that assumption, it appears you have a class like:
static class MyClass
{
public static IIndexer Section { get; }
}
IIndexer in this case could be any type that implements an indexer property that takes a string and returns a type that has a method named Process which in turn returns a bool. IIndexer could theoretically look like:
interface IIndexer
{
ISomething this[string] { get; }
}
Next we'll fill in the ISomething blank above with a simple IProcess interface so we don't have to know anything about your specific implementation:
interface IProcess
{
bool Process();
}
So now the indexer above can be changed to:
IProcess this[string] { get; }
Of course, none of the above has any real executable code, but outlines the objects necessary to do what you're after. Now when you go to run your code using your fulfilled contracts the call chain is pretty simple:
bool result1 = MyClass.Section["area1"].Process();
// MyClass.Section > IIndexer.this[string] > IProcess.Process
To POC the idea, a good way to mock the IIndexer implementation might be to use Dictionary<string, IProcess> as it'll give you a usable indexer for your purposes.