Why is C# statically typed?

2020-01-28 03:12发布

I am a PHP web programmer who is trying to learn C#.

I would like to know why C# requires me to specify the data type when creating a variable.

Class classInstance = new Class();

Why do we need to know the data type before a class instance?

17条回答
做个烂人
2楼-- · 2020-01-28 03:45

c# is a strongly-typed language, like c++ or java. Therefore it needs to know the type of the variable. you can fudge it a bit in c# 3.0 via the var keyword. That lets the compiler infer the type.

查看更多
Summer. ? 凉城
3楼-- · 2020-01-28 03:45

One things that hasn't been mentioned is that C# is a CLS (Common Language Specification) compliant language. This is a set of rules that a .NET language has to adhere to in order to be interopable with other .NET languages.

So really C# is just keeping to these rules. To quote this MSDN article:

The CLS helps enhance and ensure language interoperability by defining a set of features that developers can rely on to be available in a wide variety of languages. The CLS also establishes requirements for CLS compliance; these help you determine whether your managed code conforms to the CLS and to what extent a given tool supports the development of managed code that uses CLS features.

If your component uses only CLS features in the API that it exposes to other code (including derived classes), the component is guaranteed to be accessible from any programming language that supports the CLS. Components that adhere to the CLS rules and use only the features included in the CLS are said to be CLS-compliant components

Part of the CLS is the CTS the Common Type System.

If that's not enough acronyms for you then there's a tonne more in .NET such as CLI, ILasm/MSIL, CLR, BCL, FCL,

查看更多
男人必须洒脱
4楼-- · 2020-01-28 03:49

Because C# is a strongly typed language

查看更多
疯言疯语
5楼-- · 2020-01-28 03:50

It's simply how the language was designed. C# is a C-style language and follows in the pattern of having types on the left.

In C# 3.0 and up you can kind of get around this in many cases with local type inference.

var variable = new SomeClass();

But at the same time you could also argue that you are still declaring a type on the LHS. Just that you want the compiler to pick it for you.

EDIT

Please read this in the context of the users original question

why do we need [class name] before a variable name?

I wanted to comment on several other answers in this thread. A lot of people are giving "C# is statically type" as an answer. While the statement is true (C# is statically typed), it is almost completely unrelated to the question. Static typing does not necessitate a type name being to the left of the variable name. Sure it can help but that is a language designer choice not a necessary feature of static typed languages.

These is easily provable by considering other statically typed languages such as F#. Types in F# appear on the right of a variable name and very often can be altogether ommitted. There are also several counter examples. PowerShell for instance is extremely dynamic and puts all of its type, if included, on the left.

查看更多
Evening l夕情丶
6楼-- · 2020-01-28 03:54

One of the main reasons is that you can specify different types as long as the type on the left hand side of the assignment is a parent type of the type on the left (or an interface that is implemented on that type).

For example given the following types:

class Foo { }
class Bar : Foo { }
interface IBaz { }
class Baz : IBaz { }

C# allows you to do this:

Foo f = new Bar();
IBaz b = new Baz();

Yes, in most cases the compiler could infer the type of the variable from the assignment (like with the var keyword) but it doesn't for the reason I have shown above.

Edit: As a point of order - while C# is strongly-typed the important distinction (as far as this discussion is concerned) is that it is in fact also a statically-typed language. In other words the C# compiler does static type checking at compilation time.

查看更多
ら.Afraid
7楼-- · 2020-01-28 03:55

Why do we need to know the data type before a class instance?

You don't! Read from right to left. You create the variable and then you store it in a type safe variable so you know what type that variable is for later use.

Consider the following snippet, it would be a nightmare to debug if you didn't receive the errors until runtime.

 void FunctionCalledVeryUnfrequently()
 {
   ClassA a = new ClassA();
   ClassB b = new ClassB();
   ClassA a2 = new ClassB(); //COMPILER ERROR(thank god)

   //100 lines of code

   DoStuffWithA(a);
   DoStuffWithA(b);      //COMPILER ERROR(thank god)
   DoStuffWithA(a2);
 }

When you'r thinking you can replace the new Class() with a number or a string and the syntax will make much more sense. The following example might be a bit verbose but might help to understand why it's designed the way it is.

   string s = "abc";
   string s2 = new string(new char[]{'a', 'b', 'c'});
   //Does exactly the same thing

   DoStuffWithAString("abc");
   DoStuffWithAString(new string(new char[]{'a', 'b', 'c'}));
   //Does exactly the same thing
查看更多
登录 后发表回答