Code By Topic

Difference between while and do while loop in C#

By // No comments:
In C# to perform the repetitive task you are free to use For, Foreach, While,or DoWhile loop. This loop is also called as C# Iteration Constructs.In This topic we will discuss about the difference between while and do while loop. While loop and do while is almost same behaviour and difference i mentioned below. Syntax is little bit different for while and do while loop.

Difference between while & do while in C#

While Do While
Syntax
While(condition)
{
//Do Code Here
}
do{
// Do Code Here
}while(condition);

Nature & Execution Time
Takes less time to execute and the code is shorter. Takes more time to execute and code becomes longer. 
Remember
No semicolon(;) is used in last You have to use semicolon(;) in last as mentioned in syntax.
What First
Condition is checked first. Condition is checked later.
Important Point
Since condition is checked first, statements may or may not get executed. Since condition is checked later, the body statements will execute at least once.

Check Datatype in C#

By // No comments:
We already know about the datatype in C#. Some very commonly used data type is string, int, bool, short, long. In this topic we will discuss about how to know what datatype used in assigned variable. In C# we know that each and every class inherited with Object class by default. In this Object Class Virtual method and  Type is there and one of Type is GetType(). We will discuss later about Type in C#.
So with GetType() we are able to find out the type of datatype like is it System.String, System.Int32 and so on. Please find below the code as well as their output. You can also just copy and paste code and check with different way in your Visual Studio IDE.




using System;

namespace CheckDataType
{
    class Program
    {
        static void Main(string[] args)
        {
            char charType = 'A';
            string stringType = "Code By Topic";
            Int16 shortType = 1987;
            Int32 intType = 2010;
            Int64 longType = 2018;
            Boolean boolType = true;
            Console.WriteLine(charType.GetType());
            Console.WriteLine(stringType.GetType());
            Console.WriteLine(shortType.GetType());
            Console.WriteLine(intType.GetType());
            Console.WriteLine(longType.GetType());
            Console.WriteLine(boolType.GetType()); 
     Console.ReadLine();     
 }
    }
}

How to check MSIL with ILDASM

By // No comments:
The ILDASM (IL  Disassembler ) is a very good tool for those who wants to look of IL code. Basically IL code is Intermidate Language it also known as MSIL (Microsoft Intermediate Language) This tool is used to view the assembly content for all the code components .  This tools is installed with the your Visual Studio and in a traditional way, can be access from the Visual Studio Command Prompt.  In this post you will learn how you can open the same tool inside Visual Studio by using a shortcut key.
So,Now lets see the traditional way, To open the ILDASM follow the below steps

1. Open the Visual Studio Command Prompt
2. Run ILDASM Command.
Here is a sample screen shots of application opened using ILDASM.
MSIL Code
Here is the problem, whenever you need this tool, ever time you have to open the Visual Studio command prompt and then run it like above few steps. What if, if you could do it directly from Visual Studio ? First question is it is possible answer is yes then now let see how to do the same

Well, follow the below steps to configure your Visual Studio to launch the ILDASM tool.

Navigate to Tools –> External tools


Visual Studio ILDASM

Click on the “Add” button, and then provide Title, Command ( Which is the physical path of your ILDASM Tool, and then set the Initial Directory as $(TargetDir) . Please note down the path which are in command section in below screen

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\ildasm.exe
above path is vary from .Net framework to .net framework. Choose the highest version like if  \Microsoft SDKs\Windows\v10.0A  & \Microsoft SDKs\Windows\v11.0A then choose V11.0. Same for NETFX 4.7.1 Tools. 


Add ILDASM in Visual Studio

That’s it. You are done ! Click on Apply. Navigate to Tool menu, and now you should be able to see the “ILDASM (IL Disassembler )” option along with all other external tool.
Display ILDASM in Visual Studio
Hope now picture is clear how to add ILDASM in your Visual Studio IDE

Basic Building Block Of .Net

By // No comments:
We already discuss about the what is .Net framework. Now lets understand the basic building block of  .Net. We know that there are lots of language supported in .Net. But here we go with two mostly used language VB.Net and C#. We all do the programming in  Visual Studio .NET so first lets understand what is Visual Studio .NET it is a new development environment and a rapid application development (RAD) tool that is fully supported by the MSDN developer service and Windows servers.Also it is an integrated development environment (IDE) that is common to all the Microsoft programming languages in the .NET Framework.
Basically there are three important point which we have to know about the starting phase of .Net
CLR: COMMON LANGUAGE RUNTIME
CTS: COMMON TYPE SPECIFICATION
CLS: COMMON LANGUAGE SPECIFICATION
MSIL: MICROSOFT INTERMEDIATE LANGUAGE
JIT:  JUST IN TIME COMPILER 

Now lets discuss each and every point one by one here i give a brief explanation we will discuss in detail later

Common Language Runtime (CLR):
The most important part of the .NET Framework is the .Net Common Language Runtime (CLR) also called .Net Runtime in short. It is a framework layer that resides above the Operating System and handles/manages the execution of the .NET applications. Our .Net programs don't directly communicate with the Operating System but through CLR.

MSIL (Microsoft Intermediate Language):
When we compile our .Net Program using any .Net compliant language like (C#, VB.NET, C++.NET) it does not get converted into the executable binary code but to an intermediate code, called MSIL or IL in short, understandable by CLR. MSIL is an OS and H/w independent code. When the program needs to be executed, this MSIL or intermediate code is converted to binary executable code, called native code. The presence of IL makes it possible the Cross Language Relationship as all the .Net compliant languages produce the similar standard IL code.

Just In Time Compilers (JIT):
When our IL compiled code needs to be executed, CLR invokes JIT compilers which compile the IL code to native executable code (.exe or .dll) for the specific machine and OS. JITers in many ways are different from traditional compilers as they, as their name suggests, compile the IL to native code only when desired e.g., when a function is called, IL of function's body is converted to native code; just in time of need. So, the part of code that is not used by particular run is not converted to native code. If some IL code is converted to native code then the next time when its needed to be used, the CLR uses the same copy without re-compiling. So, if a program runs for sometime, then it won't have any just in time performance penalty. As JITers are aware of processor and OS exactly at runtime, they can optimize the code extremely efficiently resulting in very robust applications. Also, since JITer knows the exact current state of executable code, they can also optimize the code by in-lining small function calls (like replacing body of small function when its called in a loop, saving the function call time).

Common Language Specification (CLS):
Earlier we used the term '.NET Compliant Language' and stated that all the .NET compliant languages can make use of CLR and FCL. But what makes a language '.NET compliant language'? The answer is Common Language Specification (CLS). Microsoft has released a small set of specification that each language should meet to qualify as a .NET Compliant Language. As IL is a very rich language, it is not necessary for a language to implement all the IL functionality, rather it meets the small subset of it, CLS, to qualify as a .NET compliant language, which is the reason why so many languages (procedural and OO) are now running under .Net umbrella. CLS basically addresses to language design issues and lays certain standards like there should be no global function declaration, no pointers, no multiple inheritance and things like that. The important point to note here is that if you keep your code within CLS boundary, your code is guaranteed to be usable in any other .Net language.

Common Type System (CTS):
.NET also defines a Common Type System (CTS). Like CLS, CTS is also a set of standards. CTS defines the basic data types that IL understands. Each .NET compliant language should map its data types to these standard data types. This makes it possible for the 2 languages to communicate with each other by passing/receiving parameters to/from each other. For example, CTS defines a type Int32, an integral data type of 32 bits (4 bytes) which is mapped by C# through int and VB.Net through its Integer data type.

Garbage Collector (GC):
CLR also contains Garbage Collector (GC) which runs in a low-priority thread and checks for un-referenced dynamically allocated memory space. If it finds some data that is no more referenced by any variable/reference, it re-claims it and returns the occupied memory back to the Operating System; so that it can be used by other programs as necessary. The presence of standard Garbage Collector frees the programmer from keeping track of dangling data.

What is .Net Framwork

By // No comments:
The .Net framework is a development platform and its developed by Microsoft. The framework was use to create applications like window form, window services, MVC project,Asp.net project and console programming, which would run on the Windows Platform. The first version of the .Net framework was released in the year 2000. The version was called .Net framework 1.0. The .Net framework has come a long way since then, and the current version is move more than 4.6.Some Key Features Of that Framework which will tell you how .Net is useful

  •  Make a Data Driven Web Application whether it is with ASP.NET or The ASP.NET MVC.
  • Create a Server Application or Web Service Which responds to the request made by the Users.(WEB API || WCF || WEB Services) 
  • Create a Desktop Application for Windows Operating System (WPF). 
  • Windows Presentation Foundation 5.Create Windows Mobile Apps.
  • ASP.Net Core 1 is also Open Source and is Cross Platform that is .Net framework can now run on Windows, Linux ,Mac. 
  • With the help of Xamarin you can create Native Android and IOS app using C# OR some other language.
  • Developers can choose from a variety of programming languages available on the .Net platform. The most common one's are VB.Net and C#.  
The following design principles of the .Net framework is makes it very relevant to create .Net based applications.

Interoperability- The .Net framework provides a lot of backward support. Suppose if you had an application built on an older version of the .Net framework, say 2.0. And if you tried to run the same application on a machine which had the higher version of the .Net framework, say 3.5. The application would still work. This is because with every release, Microsoft ensures that older framework versions gel well with the latest version.
Portability- Applications built on the .Net framework can be made to work on any Windows platform. And now in recent times, Microsoft is also envisioning to make Microsoft products work on other platforms, such as iOS and Linux.
Security - The .NET Framework has a good security mechanism. The in-built security mechanism helps in both validation and verification of applications. Every application can explicitly define their security mechanism. Each security mechanism is used to grant the user access to the code or to the running program.
Memory management- The Common Language runtime does all the work or memory management. The .Net framework has all the capability to see those resources, which are not used by a running program. It would then release those resources accordingly. This is done via a program called the "Garbage Collector" which runs as part of the .Net framework.
The garbage collector runs at regular intervals and keeps on checking which system resources are not utilized, and frees them accordingly.
Simplified deployment- The .Net framework also have tools, which can be used to package applications built on the .Net framework. These packages can then be distributed to client machines. The packages would then automatically install the application.