Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C# a strongly typed or a weakly typed language? [duplicate]

Can someone please clarify if C# a strongly typed or a weakly typed language? And explain the answer why.

if I have a function called concat that can take any object is this then considered weakly typed?

function concat(Object stuff)
{
   //do something here to stuff
}
like image 524
stackoverflow Avatar asked Feb 18 '13 21:02

stackoverflow


2 Answers

From http://ericlippert.com/2012/10/15/is-c-a-strongly-typed-or-a-weakly-typed-language/

Is C# a strongly typed or a weakly typed language?

Yes.

That is unhelpful.

I don't doubt it. Interestingly, if you rephrased the question as an "and" question, the answer would be the same.

What? You mean, is C# a strongly typed and a weakly typed language?

Yes, C# is a strongly typed language and a weakly typed language.

I'm confused.

Me too. Perhaps you should tell me precisely what you mean by "strongly typed" and "weakly typed".

Um. I don't actually know what I mean by those terms, so perhaps that is the question I should be asking. What does it really mean for a language to be "weakly typed" or "strongly typed"?

"Weakly typed" means "this language uses a type verification system that I find distasteful", and "strongly typed" means "this language uses a type system that I find attractive".

like image 51
Gideon Avatar answered Oct 04 '22 02:10

Gideon


C# is strongly typed.

ECMA-334 Defines C# as "C# (pronounced “C Sharp”) is a simple, modern, object oriented, and type-safe programming language."

Wikipedia defines type safety

Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible.

Wikipedia defines strong-typing as

In computer science and computer programming, a type system is said to feature strong typing when it specifies one or more restrictions on how operations involving values of different data types can be intermixed. The opposite of strong typing is weak typing.

Perhaps it's better to ask if C# is a type-safe language since nobody can agree on what "strong" and "weak typing" really mean if the compiler will do type checking.

C# does have some dynamic language like constructs available but remarkably these are still type-safe at compile time.

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

http://msdn.microsoft.com/en-us/library/bb383973.aspx

The dynamic keyword basically works the same way except it is evaluated at run-time instead of at compile time as the case with var.

Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation.

http://msdn.microsoft.com/en-us/library/dd264736.aspx

like image 38
Mr. Young Avatar answered Oct 04 '22 03:10

Mr. Young