Understanding 'var' Type in C#: Strong or Weak Type?

  • Thread starter haki
  • Start date
  • Tags
    Type Weak
In summary, type systems in programming languages such as Java and C# require the exact type of a variable to be specified. However, in C# 3.0, the var variable was introduced which allows for the compiler to infer the type at compile time. This has led to debates on whether C# is still considered a strongly typed language or not. Some argue that var is a weaker type because it can substitute any type, while others argue that it is still strongly typed because the compiler still determines the type at compile time and does not require a runtime check. The use of var can also be convenient for template programming, where type expressions can become verbose and difficult to understand. Overall, the use of var is a matter of preference and
  • #1
haki
161
0
As far as I understand type systems in strongly typed system such as the one found in Java you must specify the exact type of the variable. Like e.g.

int x = 5; // strongly typed

also this would fail

Object x = 5;
Object y = "Not soo strong are we?";
Object z = x + y; // would throw a compiler error, since + operation is not defined on Object + Object, it is only defined on either Number + Number or String + String or String + Object

Now I said to a friend that C# is no longer strongly typed since in 3.0 you have the var variable which is more of a placeholder than a type, what kind of type is var? var can substitue any type therefore I would say that is weak type. He didn't agree on that one. I am left a bit puzzled. Is var type weak or not? I would say it is.
 
Technology news on Phys.org
  • #2
It's still strongly typed because the compiler infers the actual type at compile time. In no case would it need a runtime check; I think in ambiguous cases the type must be supplied. It's just a matter of convenience I guess, but I don't understand why it is seen as a good thing. To me it seems that it would make code more difficult to understand.
 
  • #3
Thanks for the answer. Yeah, I wouldn't dream of using var exept if I would write JavaScript.
 
  • #4
var is a feature that would be very useful for template programming in C++.

Type expressions can get quite verbose; here's a simple one:

Code:
// Print everything in a container
template <typename Container>
void print_all(const Container &c) {
  typename Container::const_iterator iter = c.begin();
  while(iter != c.end()) {
    std::cout << (*iter++) << std::endl;
  }
}

Sometimes, you have to write stuff like typename Container::const_iterator a lot; it would be very convenient to be able to just write:

var iter = c.begin()


Even worse, sometimes you aren't even supposed to know the return type of an expression. For example, in linear algebra packages, if I write an expression like:

Code:
template <typename T>
void do_something(boost::numeric::ublas::vector<T> &a,
         const boost::numeric::ublas::vector<T> &b,
         const boost::numeric::ublas::vector<T> &c)
{
  a = 3 * b + 4 * c;
}

ublas generates efficient code for this function through the magic of expression templates: each of those arithmetic operations constructs a type that "knows" the parse tree of the expression. For example, the return type of 3*b is


typename vector_binary_scalar1_traits<int, vector<T>, scalar_multiplies<int, typename vector<T>::value_type> >::result_type


which is not only something you wouldn't want to have to type, it's something that you really ought not to even know about! If I wanted to do something like

Code:
var e1 = 3 * b;
var e2 = 4 * c;
a = e1 + e2;

then a keyword like var is the only reasonable option for doing so.
 

FAQ: Understanding 'var' Type in C#: Strong or Weak Type?

Is "var" considered a weak type in programming?

Yes, "var" is generally considered a weak type in programming languages that support type inference. This means that the type of the variable is not explicitly declared and can change based on the value it is assigned.

What is the difference between "var" and "let"?

The main difference between "var" and "let" is that "var" is function-scoped, meaning it can be accessed anywhere within the function it is declared in. "let" is block-scoped, meaning it can only be accessed within the block of code it is declared in. Additionally, "let" does not support hoisting like "var" does.

When should I use "var" instead of "let"?

"Var" should be used when you want the variable to have function scope or when you need to access it before it is declared (due to hoisting). It can also be used when you want the variable to be reassigned multiple times.

Is "var" still commonly used in modern programming?

No, "var" is not as commonly used in modern programming as it was in the past. With the introduction of "let" and "const", which offer more specific scoping and stricter rules, "var" is being used less and less.

Can "var" be used in all programming languages?

No, "var" is not a universal keyword and is not supported in all programming languages. It is most commonly used in languages that support type inference, such as JavaScript and Python. Other languages may have similar keywords with different functionality.

Back
Top