1 / 20

C# Console Application

C# Console Application. C# syntax Overview. C# Syntax. The following sample shows different ways you can declare a variable: int a; int salary, income Tax, sum; int count = 10; string name; string fullName= "Little John";. C# Syntax. Loop Statements while

brock
Download Presentation

C# Console Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Console Application C# syntax Overview

  2. C# Syntax • The following sample shows different ways you can declare a variable: • int a;int salary, income Tax, sum;int count = 10; • string name;string fullName= "Little John";

  3. C# Syntax • Loop Statements • while • int i = 0;while ( i < 5 ){Console.WriteLine ( i );++i;} • The above loop repeates 5 times and prints the value of i.The output of above code would be like this : • 01234

  4. C# Syntax • for • for ( int i = 0; i < 5; i++ ){Console.WriteLine ( i );} • The above loop repeates 5 times just like the while loop and prints the value of i.The output of above code would be like this : • 01234

  5. C# Syntax • do ... while • int i = 0;do{ Console.WriteLine ( i ); i++;}while ( i < 5 ); • The above loop is pretty much same as the while loop. The only difference is, the condition is checked only after executing the code inside the loop.

  6. C# Syntax • foreach • string [] names = new string[]{ "Little John", "Pete", "Jim", "Bill" };foreach ( string name in names ){ Console.WriteLine ( name );} • foreach loop can be used to iterate through a collection like array, ArrayList etc.The above code displays the following output: • Little johnPeteJimBill

  7. C# Syntax • Conditional Operators • if ... elseThis is the conditional operator, used to selectively execute portions of code, based on some conditions. • string name = "Little John";if ( name == "Jim" ){ Console.WriteLine( "you are in 'if' block" );}else{ Console.WriteLine( "you are in 'else' block" );} • in the above case, it prints : • you are in 'else' block

  8. C# Syntax • Flow Control Statements • break'break' statement is used to break out of loops ('while', 'for', switch' etc). • string [] names = new string[] { "Little John", "Pete", "Jim", "Bill" };foreach ( string name in names ){ Console.WriteLine ( name ); if ( name == "Pete" ) break;} • In the above sample, it iterates through the array of 4 items, but when it encounters the name "Pete", it exits the loop and will not continue in the loop anymore.The output of above sample would be : • Little JohnPete

  9. C# Syntax • continue'continue' statement is also used to in the loops ('while', 'for' etc). When executed, 'continue' statement will move the exection to the next iteration in the loop, without continuing the lines of code after the 'continue' inside the loop. • string [] names = new string[]{ "Little John", "Pete", "Jim", "Bill" };foreach ( string name in names ){ if ( name == "Pete" ) continue; Console.WriteLine ( name );} • In the above sample, when the value of name is "Pete", it executes the 'continue' which will change the execution to the next iteration, without executing the lines below it. So, it will not print the name, if the name is "Pete".The output of above sample would be : • Little JohnJimBill

  10. C# Syntax • switchif you have to writeseveral if...else conditions in your code, switch statement is a better way of doing it. The following sample is self explanatory: • int i = 3;switch ( i ){ case 5: Console.WriteLine( "Value of i is : " + 5 ); break; case 6: Console.WriteLine( "Value of i is : " + 6 ); break; case 3: Console.WriteLine( "Value of i is : " + 3 ); break; case 4: Console.WriteLine( "Value of i is : " + 4 ); break; default: Console.WriteLine( "Value of i is : " + i ); break;}

  11. C# Syntax • In the above sample, depending on the value of the conditional item, it executes appripriate case. In our code, since the value of i is 3, it executes the third case. The output will be as shown below: • Value of i is : 3

  12. New Topic Data Type in C#

  13. Data type in c# • DataTypes are the basic building block of any language. • Microsoft has tried to standardise the datatypes in .NET framework by introducing a limited, fixed set of types that can be used to represent almost anything in programming world. • C++ was very rich in datatypes, but that leads to confusion too. • Especially, when you write components that may be consumed by applications written in other platforms, you have to make sure the types used are compatible with other platforms too!

  14. Data type in c# • .NET types start from a clean slate. All .NET languages share the same types. • They are all compatible and no worries. • This means, you can call C# code from VB.NET and vice versa, without worrying about type conversions.

  15. Data type in c# • .NET data types are either structures or classes, part of the System namespace. For example, the following data types are implemented as struct in .NET: • Int16 • Int32 • Double(String is implemented as a class in .NET, for various reasons.)If you are not very familiar with struct and class, don't worry about it. You can just use them as if they are simple data types.

  16. Data type in c# • Here is how you can declare variables of type Int, Double and String: • Int16 age, employeeNumber; • Double salary; • String name, address;

  17. Data Type in C# • You can use any of the .NET data types directly in any .NET language - in C#, VB.NET or xyz.NET. • But in addition to the .NET types, each language provides a set of primitive types, which map to the corresponding types in .NET class library. • This is why you may see some people use string and some others use String. There is no big difference. string is a primitive data type in C# and String is the corresponding class in .NET class library • The string in C# is mapped to the class in .NET class library. So, whether you use string or String, there is no real difference. • The following list shows the list of data types available in C# and their corresponding class/struct in .NET class library

  18. Data type in c#

  19. Data type in c# • In C# data types are classified into two : • value types • reference types • The following tables shows some of the differences between values types and reference types.

  20. Data type in C#

More Related