C# .NET 框架中的强类型是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10779804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:13:54  来源:igfitidea点击:

What does strongly typed means in .NET framework?

c#.netado.net

提问by Tony

This morning was going through a book where I found a paragraph as stated below :

今天早上正在阅读一本书,在那里我发现了一段如下所述:

Each data field in a table is a strongly typed data member, fully compliant with .NET's Common Type System.

表中的每个数据字段都是强类型数据成员,完全符合 .NET 的通用类型系统。

Does the above lines means " that objects written in different languages can interact with each other like "

上面几行是否意味着“用不同语言编写的对象可以像“一样相互交互”

And if it means the above lines what does exactly the above line means by saying different languages can interact with each other like

如果这意味着上面几行,上面这行的意思是说不同的语言可以像这样相互交互

I am trying to work out with an example but no success till now.

我正在尝试用一个例子来解决,但直到现在还没有成功。

Or is it something that i am missing and need to know. Please help me to understand.

或者它是我遗漏的东西,需要知道。请帮我理解。

Thanks in advance

提前致谢

采纳答案by FosterZ

For e.g you cannot Multiply or Divide two different types i.e String vs Integer

例如,您不能乘以或除以两种不同的类型,即 String vs Integer

var answer = 1 * "1"; // you cannot do this

You have to explicity cast it, this is known as strongly typed

你必须明确地强制转换它,这被称为强类型

where as if you see in php

就像你在 php 中看到的一样

$x = "3" * 1; // is correct in php

So here you dont need to explicitly cast it.

所以在这里你不需要明确地投射它。

回答by Ignacio Vazquez-Abrams

No. It means that 1and "1"(or any other number and string for that matter) are different values that cannot be casually interchanged in expressions.

不。这意味着1"1"(或任何其他数字和字符串)是不同的值,不能在表达式中随意互换。

回答by user381624

"fully compliant with .NET's Common Type System" means that the data types are usable in any .NET language. So if you created a class that exposes a property in c# that is CTS compliant, that class can be consumed from say VB.net.

“完全符合 .NET 的通用类型系统”意味着数据类型可用于任何 .NET 语言。因此,如果您创建了一个在 c# 中公开一个符合 CTS 的属性的类,则可以从 VB.net 中使用该类。

"Each data field in a table is a strongly typed data member" means that you can rely on the type of the value in the table, and you would have to cast it to another type if that was required. You can't do implicit casting.

“表中的每个数据字段都是强类型数据成员”意味着您可以依赖表中值的类型,如果需要,您必须将其转换为另一种类型。您不能进行隐式转换。

回答by Kishore Kumar

This means, if there are two variables of different types, you have to cast them, to make an operation executable.

这意味着,如果有两个不同类型的变量,则必须对它们进行强制转换,以使操作可执行。

Else it will throw an exception.

否则会抛出异常。

回答by Atish Dipongkor - MVP

When we say something is strongly typed we mean that the type of the object is known and available.

当我们说某事物是强类型时,我们的意思是该对象的类型是已知且可用的。

Let say I have a function like following

假设我有一个如下功能

public int Add(int a, int b){
 return a+b;
}

We can call this function like

我们可以像这样调用这个函数

int result = Add(5,4);

But we can not do like following

但是我们不能像下面这样

int result = Add(5.2,4.5); // We will get here compilation error.

C# (and C++ and many other languages) is strongly typed because the compiler will detect and flag these errors at compilation time.

C#(以及 C++ 和许多其他语言)是强类型的,因为编译器会在编译时检测并标记这些错误。

See here

这里