Sunday, 27 December 2020

Keywords and Variables

                                                                                                                                  #5 minutes of reading

 

In this article I will explain keywords and variables in C#. Every language has keywords and variables.

Keyword is unique words and it has some special meaning. These unique/reserved words called as keywords. Below are few keywords from C#.  for example, public is keyword which tells compiler it is specified and used for declaring scope of method or class. Will discuss lots of keywords in coming article.   

class, const, new, static, void, …etc. are examples of keywords

 

Variables are used to hold value which C# program needs for further processing. We can say variable is container to hold data for C# program.

Let’s create addition and subtraction C# program without variable. You can see drawback of program without variable its hardcoded and every time Calculation function print same result. How can we make it generic? the answer is using variable

 

Below program with variable. Now I can pass number in calculation method and its not hardcoded now we have different result compared to previous screen. Highlighted int a and int b is variable.


Variable is container which hold value and variable can be used in several places in C# program.

 C# variables are categorized into two types:

      1.       Value Type

      2.       Reference Type

 

Value Type:

Data (5) stored directly in stack memory along with variable address(num1) is called as value type. Let’s understand this with example


 

Int, float, double, char, bool, …etc. are examples of value type. Value type derived from namespace System.ValueType. Don’t worry if you don’t understand what namespace is, will cover in coming future article.


I have created small program and covered few most common types and range of type in above screen. If number exceed 2,147,483,648 in that case integer won’t support as limit of integer is 2,147,483,647, we need to switch to float type. If number crossed limit of float, then switch to double.  Types have range start and end choose appropriate type for your program.

  

Reference Type:

Data (John) is not stored along with name of variable (name) in stack memory but the address of heap memory stored in stack along with variable name(address). String, array and class is example of reference type.



As of now understand the concept of value type and reference type later will cover all common types from Value and reference types.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home