Advanced_C#_Skill_Implicit Typing




Today , I want to introduce a little bit about what implicit typing is

and How to use it a few of the gotchas

and what it's really going to do for you over the long term of your development career.

【PS: gotchas means some unexpected features which make you feel so surprised.】

Let's go ahead and jump into some code and I'll show you what I'm talking about.


So here we are in Visual Studio2015


Im usiung the enterprise edition but you can absolutely use any of the free exopress or community editions that you can download from msdn official website.



The fisrt thing I'm gonna to do is I'm going to create a new project 

So I'll select File --> New Project 

and  I will make sure that C sharp is selected and choose the Console Application.






So now  we have our console application and the boilerplate code comes with it 

【PS : boilerplate means template】






Let's getting start for implicit typing but before we start

I'd like to show you a little bit where we've come from 

so let's say we are writing some code that had a little bit to do with us working with a person .

We needed  to define some other properties or some fields or just work with some of those 

characteristics of a person within our code.


 The way we used to do that is we would define these characteristics by explicitly.

【PS : explicitly means clearly 】


stating the types of the variables 




Several versions ago of C# language Microsoft

introduced this concept of   implicit typing which really allowed us to utilize 

the compiler and visual studio to take a little bit of the explicit nature of creating 

variables and initializing them out of our hands.




the way that we were able to do this was with the introduction of the VAR.

keyword  so  what I can do with the type parts of the declaration and in this initialization

of the first name variable is I can say this is a VAR which is really 

stating to the complier I would like to create a variable not a very intense will discuss the 

difference in just a moment that with athe name of first name and I want to initialize it 

to the string "Join" now this is very important 






we're going to come back to  this just a moment 


So let's say we wanted to do the same thing with an integer we don't have to 

be using strings in order to use the VAR keyword we can use it for any types within 

the C# or within the .NET frameowrk


so we could do it with age as well.





Becarefully !


Before you use  VAR  ,  ensure the variable has been initialized.

Because we cannot use implicity type local variables without initializing them

the reason we need to that is because the compilier needs to be able to look at this 

initialization step and figure out the type of it .


















留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header