我被告知static方法是隐含的final,因此不能被覆盖.真的吗?
有人可以提供一个更好的覆盖静态方法的例子吗?
如果静态方法只是类方法,那么拥有它们的真正用途是什么?
我对使用方法与C#中的对象进行交互的不同方式有点困惑,特别是以下主要设计差异和后果:
例:
public class MyPoint
{
public double x { get; set; }
public double y { get; set; }
public double? DistanceFrom(MyPoint p)
{
if (p != null)
{
return Math.Sqrt(Math.Pow(this.x - p.x, 2) + Math.Pow(this.y - p.y, 2));
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您只需将方法放在类定义中就可以完成所需的结果,那么为什么POCO与静态辅助类或扩展方法结合起来会更好?
我有静态类,有静态方法,如下所示:
public static StaticTest
{
public static void DoSomeWork()
{
/// Do Some work
}
}
Run Code Online (Sandbox Code Playgroud)
当DoSomeWork()函数被调用时,如何CLR管理功能的引用,因为它是明显的静态类的实例不能创建?
在这种情况下调用函数的场景背后的机制是什么?
我有一个公共课叫MyClass.cs.它有3种方法:
public class MyClass
{
public IEnumerable<MyDto> PublicA(bool useCache = true)
{
//Call an external resource
//some code
}
public IEnumerable<AnotherDto> PublicB()
{
//some code
var x= MyPrivateMethod(input);
//some code
}
private IEnumerable<AnotherDto> MyPrivateMethod(IEnumerable<SomeDto>)
{
//return Mapped data from IEnumerable<SomeDto> to IEnumerable<AnotherDto>
}
}
Run Code Online (Sandbox Code Playgroud)
我使用ReSharper作为重构工具.它建议static用于MyPrivateMethod.
private static IEnumerable<AnotherDto> MyPrivateMethod(IEnumerable<SomeDto>)
Run Code Online (Sandbox Code Playgroud)
但是这个关键字的用法是什么?由于该方法是私有的,因此不会在其他想要使用MyClass实例的类中使用.
我测试并发现,当我使用static关键字时MyPrivateMethod,我不能调用非私有静态的类的任何其他方法.但我还不知道用法是什么?例如,存储或时间优化是否有任何好处?
如何获取以下程序来打印sumOfNumbers方法的结果?我无法在main方法中调用此方法,我不知道为什么.谁能解释我做错了什么?
class Program
{
static void Main(string[] args)
{
//Console.WriteLine();
//Console.ReadLine();
}
private int sumOfNumbers (int x, int y)
{
return x + y;
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在构建一个 ASP.NET Web 应用程序。我想创建一些静态方法作为辅助方法。这是个好主意还是我以后会遇到问题?没有字段或属性。只是方法,有些有返回类型,有些没有返回类型。
静态方法是否在所有用户之间共享(例如字段和属性)还是唯一的?
private static string userName;
public static string UserName
{
get
{
if (User.Identity.IsAuthenticated)
{
if (userName == "" || userName == null)
{
userName = User.Identity.Name;
}
return userName;
}
else
{
throw new ArgumentNullException("Illegal Access", "You're not login or authorize to perform such task");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我已经学习了两个星期的C#,虽然这不是我的第一语言.我一直在想静态词.我知道我早就应该研究过这个词了......但这是我第一次意识到这个词对我来说有多么混乱.我读过的内容:
静态类是一个不需要实例化的类( 使用单一方法的类 - 最佳方法?).这可能有一些优点和一些关于测试,多态性等的不利因素.
但是静态词也可以应用于类,字段,方法,属性,运算符,事件和构造函数!(https://msdn.microsoft.com/en-us/library/98f28cdx%28v=vs.80%29.aspx).例:
属性:
private static string s = "";
Run Code Online (Sandbox Code Playgroud)
方法:
public static void helperMethod() {
Console.WriteLine("Whatever");
}
Run Code Online (Sandbox Code Playgroud)
static这个词是否具有全局意义,或者在代码的不同部分使用,意义可以改变吗?
我只是在使用Microsoft Virtual Academy学习C#.这是我正在使用的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Variables
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car("BWM", "745li", "Blue", 2006);
printVehicleDetails(myCar);
Console.ReadLine();
}
}
abstract class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public abstract string FormatMe();
public static void printVehicleDetails(Vehicle vehicle)
{
Console.Writeline("Here are …Run Code Online (Sandbox Code Playgroud)