public class MyClass {
public static int SomeMethod(string param){
return .....;
}
public int SomeMethod(string param){
return MyClass.SomeMethod(param);
}
}
Here when I rename class, I should rename it in all the static method calls.
In Ruby I am able to do following:
class MyClass
def self.some_method(param)
#.....
end
def some_method(param)
self.class.some_method(param)
end
end
How to do the same in C# ?
C# doesn't have similar syntax, so you should rename all the method calls.
Related
how to call static method inside non static method in c# ?
Interviewer gave me scenario :
class class1
{
public static void method1(){}
public void method2()
{
//call method1()
}
How can we do it
A normal practice is to call static method with class name.
See: Static Classes and Static Class Members (C# Programming Guide)
The static member is always accessed by the class name, not the
instance name. Only one copy of a static member exists, regardless of
how many instances of the class are created.
So your call would be like:
class1.method1();
But it is not necessary
You can call the static method without class name like:
method1();
But you can only do that inside the class which holds that static method, you can't call static method without class name outside of that class.
class1.method1();
Same as you'd call any other static method
Apparently (as Selman22 pointed out) - the classname isn't necessary.
So
method1();
would work just as well
if you call the method in the some class you just call it like this
public void method2()
{
method1();
}
but if it should been called from another class you have to precede it with the name of the class
public void method2()
{
class1.method1();
}
You type the method name out, and then compile and run it:
class class1
{
public static void method1(){}
public void method2()
{
method1()
}
}
What's the difference between static classes, and static methods? I want to learn the differences, and when I might use one vs. the other.
For example, I have a class like this:
static class ABC
{
public int a;
public void function_a()
{
a = 10;
}
}
and another class like this:
class DEF
{
public static int a;
public static void function_a()
{
a= 10;
}
}
I have used the second type of class many times, and I know the usage. What's the usage of the first example?
Your first example will not compile, a static class must have all static members.
The difference between just using some static methods and a static class is that you are telling the compiler that the class cannot be instantiated. The second example you can create an object of the DEF class even though there are no instance methods in it. The ABC class cannot be instantiated with the new operator (will get a compile-time error).
When to Use Static Classes
Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.
C#
class CompanyInfo
{
public string GetCompanyName() { return "CompanyName"; }
public string GetCompanyAddress() { return "CompanyAddress"; }
//...
}
These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:
C#
static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}
Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.
I have a function in file Code.xaml.cs.
public string send (string url)
{
//some code...
}
I want to call this function from another .cs file.
send("google.com");
But the debugger gives an error! How can I do this?
Whenever calling a dynamic method from a class, you will have to create an instance of that class.
class Test
{
public string send(string url) {}
}
class AnotherClass
{
public AnotherClass()
{
Test t = new Test();
t.send("google.com");
}
}
Otherwise you can simply use the static keyword.
public static string send(..);
If the send method isn't acting as a member function in the class that it's in, you can create a static Helper class.
public static class Helpers
{
public static string send(string url)
{
...
}
}
Then in any other .cs file you can call:
Helpers.send("www.google.com")
I have two classes, first:
public class A
{
public delegate void Function();
public string name;
public Function function;
public A(string name, Function function)
{
this.name = name;
this.function = function;
}
}
And in second:
public class B
{
public delegate void Function();
List<A> ListA;
// somewhere in function
void F()
{
ListA[i].function();
}
// ---
public void AddA(string name, Function function)
{
ListA.Add(new A(name, function)); // error here
}
}
It throws these errors:
Error 2 Argument 2: cannot convert from 'App.B.Function' to 'App.A.Function'
Error 1 The best overloaded method match for 'App.A.A(string, App.A.Function)' has some invalid arguments
How to solve this?
You have declared Function delegate twice, one inside class A and one inside B.
So they're two different types, one called App.B.Function and the other called App.A.Function.
If you want to share this delegate with both the classes, just declare it once and out of the scope of the classes (i.e. in the assembly scope) e.g.:
public delegate void Function();
public class A
{
public string name;
...
}
public class B
{
List<A> ListA;
...
}
However, in the System namespace exist a certain number of generic delegates that can be used to represent various functions. Have a look at System.Action<> and System.Func<> types.
The reason it happen is that Function is declared twice, once in each clases
Error at:
public partial class Form2 : Form
Probable cause:
public static IChromosome To<T>(this string text)
{
return (IChromosome)Convert.ChangeType(text, typeof(T));
}
Attempted (without static keyword):
public IChromosome To<T>(this string text)
{
return (IChromosome)Convert.ChangeType(text, typeof(T));
}
If you remove "this" from your parameters it should work.
public static IChromosome To<T>(this string text)
should be:
public static IChromosome To<T>(string text)
The class containing the extension must be static. Yours are in:
public partial class Form2 : Form
which is not a static class.
You need to create a class like so:
static class ExtensionHelpers
{
public static IChromosome To<T>(this string text)
{
return (IChromosome)Convert.ChangeType(text, typeof(T));
}
}
To contain the extension methods.
My issue was caused because I created a static method inside the partial class:
public partial class MainWindow : Window{
......
public static string TrimStart(this string target, string trimString)
{
string result = target;
while (result.StartsWith(trimString)){
result = result.Substring(trimString.Length);
}
return result;
}
}
When I removed the method, the error went away.
Because your containing class is not static, Extension method should be inside a static class. That class should be non nested as well. Extension Methods (C# Programming Guide)