Implicit Types on scope declaration

All

Intro

One thing that is ambiguous to me is the implicit types of languages like python and c#.  This makes Python so powerful but also very confusing AT THE SAME TIME! Python and C# are very different, one strong typed and another is weak, but it’s interesting to compare how they handle a non-explicitly variable declaration.

Btw, I always use the Microsoft official guide, it’s quite explanatory[1]. Yes, I know proprietary stuff, but come on, C# is very useful and powerful too. The performance especially got better recently (a couple of years that is getting better already and GC too)

Python

On the declaration of the variable, we don’t need to explicitly say what it is, since it’s weakly typed. On the example below, x and y are different types and are not explicitly stated.

x = 5
y = “John”
print(x)
print(y)

 AND FOR ANONYMOUS VARIABLE: In python to use an anonymous variable, we use the _ –> underscore operator.

C#

C# is a strongly-typed language! However, in C# we can use one trick to make it more flexible by using the Implicitly Type VAR!!
By using the keyword var on the declaration of a variable. It’s up to the compiler figure out what type of variable is actually on the declaration. But interestingly it must be declared and used on the same statement.

var i = 10; // Implicitly typed.

int i = 10; // Explicitly typed.

It’s possible to download straight the Programming Guide from Microsoft here [2].

In a table

Everything looks better in a table, so we have:

Python C#
Anonymous _ var
Implicitly N/A var

REFs

[1] https://docs.microsoft.com/en-us/dotnet/csharp/index

[2]https://docs.microsoft.com/en-us/dotnet/opbuildpdf/csharp/programming-guide/types/toc.pdf?branch=live

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s