I use Resharper tool in visual studio
Consider a very simple class written below.
class Test()
{
//constructors do not have any return type.
Test()
{
System.Console.WriteLine("Hello World!");
}
static Test()
{
System.Console.WriteLine("Hello World in Static constructors");
}
public void A()
{
System.Console.WriteLine("A simple function in a class");
}
}
class Program
{
static void Main(string[] args)
{
var asa = new Test(); //Implicitly Typed local variables.
asa.A();
}
}
Using var (the compiler has to infer the type of the variable from the expression on
the right side of the initialization statement).
I have some clarification questions and they are below.
- Extra burden to compiler?
- How many constructors a class can have?
- Why is static constructor called first ? (I checked out by putting a breakpoint?)
- Why not Test asa = new Test(); is not preferred by Resharper?
- Is it really a good idea to use Resharper first as a beginner? (Myself being a newbie to C and .net programming!)
Thanks in Advance.
Any extra burden to the compiler is basically irrelevant – it should not be part of your decision about whether or not to use
var. As noted in comments, it may well require slightly more work for the compiler when you use explicitly declared variable… but again, it’s not going to be significant.A class can have any number of constructors… although it will become unwieldy pretty quickly.
The static constructor will be called once, before the first use of the class (whether that’s via a static method or a constructor call). Read the C# spec for more details – section 10.12 of the C# 5 spec includes:
You can configure ReSharper to suggest alternatives, or treat them as warnings, etc. Make it work however you think it should, on this front.