intTypePromotion=1

C# Corner: C# and .NET Developer's NetworkAll Source Code ADO.NET

Chia sẻ: Thao Thao | Ngày: | Loại File: PDF | Số trang:187

0
87
lượt xem
5
download

C# Corner: C# and .NET Developer's NetworkAll Source Code ADO.NET

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

C# Beginners Introduction to C# First day with C#. This part of tutorial explains what is C#, how to install it, how to write your first program, compile and run it. Introduction to C# Working with Data Types C# and its Types Control Statements: Part 1 Control Statements: Part 2 Constructors and Destructors Properties in C# Exception Handling Using Attributes in C# Encapsulation in C# Exploring Delegates Namespaces Working with Data Types Explains data types in C# and how to work with them. Control Statements: Part 1 By Amisha Mehta This lesson shows how to use c# control statements, and the difference between these controls...

Chủ đề:
Lưu

Nội dung Text: C# Corner: C# and .NET Developer's NetworkAll Source Code ADO.NET

  1. C# Corner: C# and .NET Developer's Network All Source Code C# Beginners ADO.NET Introduction to C# Exception Introduction to C# First day with C#. This part of tutorial explains what is C#, how to install it, how to Handling write your first program, compile and run it. Working with Data Types General GDI+ Working with Data Types C# and its Types Internet Explains data types in C# and how to work with them. Control Statements: Part 1 Mobiling Multithreading Control Statements: Part 1 By Amisha Mehta Control Statements: Part 2 Networking This lesson shows how to use c# control statements, and the difference between Printing these controls in c++/ JAVA and c#. Sample includes single selection, if-else, and Constructors and Destructors Security multi case .. Properties in C# Strings & Arrays Control Statements: Part 2 By Amisha Mehta Windows Forms Exception Handling This lesson shows how to use while..do, for, for..each statements WebForms Using Attributes in C# XML.NET Language Preprocessor Directives By Vivek Gupta - Encapsulation in C# This lesson contains C# language preprocessor directrives including #if, #else, Articles #define, #undef, #line etc with sample examples.. Effective C# Exploring Delegates FAQ Properties in C#: New cover on old book by Vivek Gupta Namespaces How do I Properties are accessor methods whose job it is to retrieve and set the values of Learn C# fields. Tutorials Exception Handling By Amisha Mehta Exceptions in C# provide a structured, and type-safe way of handling both system level and application level error conditions. The exception mechanism in C# is very similar to that of C++ Source Code Articles What & Why? Properties: Part 1 by Rahul Sharma FAQ If you were programming in C or C++ before coming to C#, you'll be a little bit Learn VB.NET confused about properties. Tutorials What & Why? Properties: Part 2 by Rahul Sharma In the part 1 of this series, I discussed about the get method of the properties, with the help of which you can make your variable so that nobody can modify the value of Source Code the variable Articles Difference using Directive and Statement by Yoganand Aiyadurai FAQ Learn ASP.NET This article explains difference between using directive and using statement with Tutorials sample example. C# and its Types by G. Gnana Arun Ganesh A detailed tutorial and C# types such as Value and Reference types with sample examples. COBOL.NET Eiffel.NET Events and Delegates By Kunal Cheda FoxPro.NET Events in C# are based on delegates, the Originator defining one or more callback JScript .NET functions Learn XML Mobile & SOAP for..each: A C# Syntax which Java lacks by Ashish Banerjee My Services Open Source C# has a new iteration syntax called foreach, which I believe has been inherited VBScript .NET from Visual Basic ( correct me if I am wrong). VC++.NET C# and its features by G. Gnana Arun Ganesh Visual J# Visual Studio.NET C# is a modern, type safe programming language, object oriented language that enables programmers to quickly and easily build solutions for the microsoft .NET platform Boxing and Unboxing by G. Gnana Arun Ganesh Boxing and unboxing is a essential concept in C#’s type system. With Boxing and unboxing one can link between value-types and reference-types http://www.c-sharpcorner.com/language.asp (1 of 4) [11/27/2001 7:17:40 PM]
  2. C# Corner: C# and .NET Developer's Network About Us Understanding and Implementing Namepsaces in C# by G. Gnana Books Arun Ganesh Book Chapters Career Center Namespaces allow you to create a system to organize your code. A good way to Discussion organize your namespaces is via a hierarchical system. Downloads Operator Overloading in C# by Prasad H. Events Hosting The Source code below shows how to use OperatorOverloading in C#. Operator Links Overloading is pretty useful concept derived from C++ by C#. Magazines Using Attributes in C# by Rajadurai P. Members News Letter This article shows how to create custom attribute classes, use them in code, and Sponsors query them. Training Using Array class and IEnumeration by Rajadurai P. Tools Jobs This article illustrates the usage of Array class and IEnumerator. Array class Provides Join C# Corner methods for creating, manipulating, searching and sorting arrays, thereby serving as Our Partners the base class for all arrays in the common language runtime. Submit Code Enumerators in C# by Prasad H. Win Prizes Sample example shows you how to use enumerators in C#. Visual Inheritance in C# by Prasad H. We all know that Inheritance means a extending a class with more Features without Mindcracker worrying about the implementation of features of hidden inside the class to be inherited. Visual Inheritance Part 2 by Prasad H. We all know that Inheritance means a extending a class with more Features without worrying about the implementation of features of hidden inside the class to be inherited. Abstract Classes and Methods by Rajesh V.S. This is a detailed analysis of Abstract classes and methods in C# with some concrete examples. Constructors and Destructors by Rajesh V.S. This detailed article talks about how constructors and destructors work in C# and how to use them in your applications. Encapsulation in C# G. G. Arun Ganesh In Object Oriented programming Encapsulation is the first pace. Encapsulation is the procedure of covering up of data and functions into a single unit (called class). Method Parameters in C# G. G. Arun Ganesh This article describes different method parameters such as value parameters, reference parameters, output parameters, and parameter arrays. Understanding Properties in C# Rajesh V.S. In C#, properties are nothing but natural extension of data fields. They are usually known as ‘smart fields’ in C# community. Understanding Structures in C# Rajesh V.S. A structure in C# is simply a composite data type consisting of a number elements of other types. Understanding Enumerators in C# Rajesh V.S. An enumeration (enum) is a special form of value type, which inherits from System.Enum and supplies alternate names for the values of an underlying primitive type. Rajesh V. S. Inheritance and Polymorphism By using the concept of inheritance, it is possible to create a new class from an existing one and add new features to it. By Filip Bulovic Exploring Delegates Delegates are a kind of type safe function pointers which are actually declared as class derived from System.MulticastDelegate. By Chandra Hundigam BitWise Operations in C# C# has lots of flexibility over manipulating with bits. Before I start explaining about bit wise manipulation I would like to give some inputs on binary operations. http://www.c-sharpcorner.com/language.asp (2 of 4) [11/27/2001 7:17:40 PM]
  3. C# Corner: C# and .NET Developer's Network Rajesh V. S. Pointers in C# C# also supports pointers in a limited extent. A pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays. Danish Sami Implementing Stacks in C# With the help of C# we can also implement ADT (Abstract Data Types) with little effort. An example of ADT is a simple stack of integers. Rajesh V. S. Working with Namespaces in C# In C#, namespaces are used to logically arrange classes, structs, interfaces, enums and delegates. The namespaces in C# can be nested. That means one namespace can contain other namespaces also. Passing const parameters in C#, C++, and VB.NET by Bulent Ozkir Parameter passing to a function is extremely important in all programming languages. The desire to keep the passed parameter intact forced the compiler designers to add various keywords to Multiple inheritance in C# by Craig Breakspear Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, Will a Class fully represent an object? Interface Component Interoperability by G. Gnana Arun Ganesh The fundamental concept behind both COM and DCOM is the interface. An interface is an agreement between a client and an object about how they will communicate with each other. Difference between const and readonly The sample example shows the difference between const and readonly Submitted by Say Gin C# Language changes from .NET Beta 1 to .NET Beta 2 This article contains C# language changes from Beta 1 to Beta 2. Submitted by Vivek Gupta. Source MSDN documentation. Using out and ref parameters By Kunal Cheda When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable. The out and ref parameters The out and the ref parameters are used to return values in the same variables, that you pass an an argument of a method. These both parameters are very useful when your method needs to return more than one values. Serializing Objects in C# In simple words serialization is a process of storing the object instance to a disk file. Serialization stores state of the objecti.e. member variable values to disk. Deserialization is reverse of serialization i.e. Operator Overloading Example by Prasad The code uses the feature of Operator Overloading in C#. It shows how different operators are overloaded and can be used in a easy manner. Regular Expressions Example by Prasad The following example shows the use of Regular Expresssions in C#.This program has basic validation scripts for validation easily useable in all programs. . contact: webmaster@c-sharpcorner.com copyright © 2000 c-sharpcorner.com. All rights are reserved. See terms and condition to use this site and its contents. http://www.c-sharpcorner.com/language.asp (3 of 4) [11/27/2001 7:17:40 PM]
  4. C# Corner: C# and .NET Developer's Network Sponsors: devexpress microgold apress http://www.c-sharpcorner.com/language.asp (4 of 4) [11/27/2001 7:17:40 PM]
  5. C# Corner: C# and .NET Developer's Network Introduction to C# All Source Code ADO.NET This part of tutorial explains about C# and how to write and compile your first C# program from Exception command line. Handling General GDI+ What is C#? Internet Mobiling C# is a new programming language developed by Microsoft. C# has power of C++ since it's Multithreading derived from C and C++. It is simpler as VB. Besides that, C# is a Java like language for web Networking programming and it has some good features of Delphi too. Microsoft says, that C# is the best Printing language to develop its .NET Framework applications. Security Strings & Arrays Windows Forms Installing .NET SDK WebForms XML.NET Installing .NET SDK is first step to run C# on your machine. You can install .NET SDK on Windows - ME, Windows NT, or Windows 2000. But Windows 2000 is recommended. After selecting your OS, Articles you need to follow these steps: Effective C# FAQ How do I Install IE 5.5 q Learn C# Install Microsoft .NET Framework SDK. It's free and you can download it here. NET q Tutorials Framework SDK. After installing these you can write your code in any text editor and save it as .cs q extension. Type this in an notepad and save as my.cs. Source Code C# Compiler and Editors Articles FAQ .NET SDK Beta 1 release of Microsoft's new platform, .NET, incorporated with C# command line Learn VB.NET compiler. You must have to install .NET SDK to run a C# program. Once you install .NET SDK, Tutorials you can write your C# program in any text editor including notepad, wordpad or Visual Studio. There are some third party editors are available in the market too. Some of them are free. Check out tools section of C# Corner. Source Code Write your first C# program Articles FAQ Learn ASP.NET Writing your first C# program is similar to writing C++ applications. You open any text editor, I Tutorials described in the above paragraph and type this code. using System; class MyClass COBOL.NET { Eiffel.NET static void Main() { FoxPro.NET Console.WriteLine("Hello World!"); JScript .NET } Learn XML } Mobile & SOAP My Services Open Source Compile your first C# program VBScript .NET VC++.NET Visual J# Now use command line to compile your cs file. C# compiler takes at least one argument i.e., file Visual Studio.NET name. Say your C# file name is myclass.cs then here is command line syntax. csc myclass.cs C# compiler creates an exe file in the bin dir of your project. Just run this exe and see the output. http://www.c-sharpcorner.com/Language/cs_lang_1.asp (1 of 2) [11/27/2001 7:19:46 PM]
  6. C# Corner: C# and .NET Developer's Network Now, lets take a look of your code line by line. About Us Books Book Chapters The first line of your program is using System. Career Center Discussion Downloads using System. Events Hosting Why using System? System is a namespace which stores system classes. The Links Magazines Console class, I used in the program to display the output on the console is Members defined in the System namespace. That's why this like is there. News Letter Sponsors Training Next line is class MyClass. The class keyword in C# is used to create a new Tools class. Jobs Join C# Corner Our Partners class MyClass Submit Code { Win Prizes .... } Each class has one static void Main() function. This function is the entry point of a C# program. Mindcracker static void Main() { Console.WriteLine("Hello, C# World!"); } The WriteLine method of the Console class writes text to the console. Mahesh is Admin and the founder of C# Corner. He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over 5 years. More articles by Mahesh... contact: webmaster@c-sharpcorner.com copyright © 2000 c-sharpcorner.com. All rights are reserved. See terms and condition to use this site and its contents. Sponsors: devexpress microgold apress http://www.c-sharpcorner.com/Language/cs_lang_1.asp (2 of 2) [11/27/2001 7:19:47 PM]
  7. C# Corner: C# and .NET Developer's Network Working with Data Types All Source Code ADO.NET First thing a programmer looks for is what kind of data types a programming languages has and how to use Exception them. In this part, I will cover C# data types and how to use them in a program. Handling General GDI+ Basic Data Types Internet Mobiling Most of the data type in c# are taken from C and C++. This tables lists data types, their description, and a Multithreading sample example. Networking Printing Security Data Type Description Example Strings & Arrays object The base type of all types object obj = null; Windows Forms WebForms string String type - sequence of Unicode characters string str = "Mahesh"; XML.NET sbyte 8-bit signed integral type sbyte val = 12; - short 16-bit signed integral type short val = 12; Articles Effective C# int 32-bit signed integral type int val = 12; FAQ long 64-bit signed integral type long val1 = 12; How do I long val2 = 34L; Learn C# bool val1 = true; Tutorials Boolean type; a bool value is either true or false bool bool val2 = false; Character type; a char value is a Unicode character char char val = 'h'; byte 8-bit unsigned integral type byte val1 = 12; Source Code byte val2 = 34U; Articles ushort 16-bit unsigned integral type ushort val1 = 12; FAQ ushort val2 = 34U; Learn VB.NET uint 32-bit unsigned integral type uint val1 = 12; Tutorials uint val2 = 34U; ulong 64-bit unsigned integral type ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; Source Code ulong val4 = 78UL; Articles float Single-precision floating point type float val = 1.23F; FAQ Learn ASP.NET double Double-precision floating point type double val1 = 1.23; Tutorials double val2 = 4.56D; decimal Precise decimal type with 28 significant digits decimal val = 1.23M; Types in C# COBOL.NET Eiffel.NET FoxPro.NET C# supports two kinds of types: value types and reference types. JScript .NET Learn XML Mobile & SOAP Types Description My Services Value Types Includes simple data types such as int, char, bool, enums Open Source VBScript .NET Reference Types Includes object, class, interface, delegate, and array types VC++.NET Visual J# Visual Studio.NET Value Types- Value type objects direct contain the actual data in a variables. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other. int i = 10; Reference Types- Reference type variables stores the reference of the actual data. With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. http://www.c-sharpcorner.com/Language/cs_lang_2.asp (1 of 2) [11/27/2001 7:20:02 PM]
  8. C# Corner: C# and .NET Developer's Network About Us Books MyClass cls1 = new MyClass(); Book Chapters Career Center Data Type Conversions Discussion Downloads Events C# supports two types of conversions. Implicit conversions and explicit conversions. Hosting Links Implicit conversions are direct conversion. For example: Magazines Members News Letter int iVal = 34; Sponsors long lVal = intValue; Training Tools Jobs Explicit conversions includes type casting. conversion. For example: Join C# Corner Our Partners long lVal = 123456; Submit Code int iVal = (int) lVal; Win Prizes Mindcracker Mahesh is Admin and the founder of C# Corner. He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over 5 years. More articles by Mahesh... contact: webmaster@c-sharpcorner.com copyright © 2000 c-sharpcorner.com. All rights are reserved. See terms and condition to use this site and its contents. Sponsors: devexpress microgold apress http://www.c-sharpcorner.com/Language/cs_lang_2.asp (2 of 2) [11/27/2001 7:20:02 PM]
  9. C# Corner: C# and .NET Developer's Network Control Statements All Source Code ADO.NET Author: Amisha Mehta Exception Handling General Control Statements: GDI+ Internet Mobiling This lesson shows how to use c# control statements, and the difference between these controls in c++/ JAVA and c#. Multithreading Networking The first selection statement is programmer’s favorite “if” statement. It has three forms: Printing Security Strings & Arrays 1. Single selection Windows Forms 2. if-then-else selection WebForms 3. multi-case selection XML.NET - List: 1 IfTest.cs Articles Effective C# FAQ //understanding if statement How do I Learn C# Tutorials using System; class IfTest { Source Code public static void Main(){ Articles FAQ string s; Learn VB.NET int i; Tutorials Console.WriteLine("Enter a Number: "); s = Console.ReadLine(); i = Int32.Parse(s); Source Code //single selection Articles FAQ Learn ASP.NET if(i > 0) Tutorials Console.WriteLine("The number {0} is positive",i); //if-then-else selection COBOL.NET if(i > 0) Eiffel.NET Console.WriteLine("The number {0} is positive",i); FoxPro.NET else JScript .NET Console.WriteLine("The number {0} is not positive",i); Learn XML Mobile & SOAP //multi-case selection My Services Open Source if(i == 0) VBScript .NET Console.WriteLine("The number is zero"); VC++.NET else if(i > 0) Visual J# Console.WriteLine("The number {0} is positive",i); Visual Studio.NET else Console.WriteLine("The number {0} is negative",i); } } This program reads a number from console. Input coming from console is in string format. Int32.parse(string) is used to convert string literal to integer. http://www.c-sharpcorner.com/Language/ControlStatements1.asp (1 of 6) [11/27/2001 7:20:30 PM]
  10. C# Corner: C# and .NET Developer's Network About Us The variable i is the object of evaluation here. c++ programmers can see that the use of if statement is same in c#. Halt it. Books There is one difference. The expression in an if statement must resolve to bool value. Take a look at following code. Book Chapters Career Center Discussion list: 2 Downloads Events using System; Hosting Links class IfTest2 { Magazines Members public static void Main(){ News Letter Sponsors if(1) Training Console.WriteLine("The if statement executed"); Tools } Jobs } Join C# Corner Our Partners Submit Code Win Prizes when this code is complied by c# compiler, it will give the error “constant value 1 can not be converted to bool”. conditional or ( || ) and conditional and ( && ) operators are used in the same manner. Consider the following code. Mindcracker List: 3 LeapTest.cs //Leap year using System; class LeapTest { public static void Main(){ int year; Console.WriteLine("enter the year value (yyyy) :"); year = Int32.Parse(Console.ReadLine()); if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) Console.WriteLine("The year {0} is leap year ",year); else Console.WriteLine("The year {0} is not leap year ",year); } } Similar to if-else if-else if-else form of the if statement is the “switch” statement. List: 4 SwitchTest1.cs using System; class SwitchTest1 { public static void Main(){ int i = 1; switch(i){ case 1 : Console.WriteLine("one"); break; default : Console.WriteLine("default"); } http://www.c-sharpcorner.com/Language/ControlStatements1.asp (2 of 6) [11/27/2001 7:20:30 PM]
  11. C# Corner: C# and .NET Developer's Network } } the difference here is if you try to compile above code without putting break statement in case 1, compiler will raise an error “control can not fall through from one case label”. I’m sure c++/Java programmers are going to love this. Here is another good news. The switch expression can evaluate to following types: sbyte, byte,short, ushort, int, uint, long, ulong, char, string or enum. So the following code is perfectly OK with c# compiler. List: 5 SwitchTest2.cs using System; class SwitchTest2 { public static void Main(){ string day; Console.WriteLine("enter the day :"); day = Console.ReadLine(); switch(day){ case "Mon" : case "Monday" : Console.WriteLine("day is Monday: go to work"); break; default : Console.WriteLine("default"); } } } Control Statements – Loops This lesson shows you how to use c# looping statements. In c#, there are four iteration constructs rather than three. There are familiar for, do, while loops and a new one from Visual basic, foreach. Let us start with while loop. The Do and While Statements List: 1 WhileTest.cs //find out the number of digits in a given number. using System; class WhileTest { public static void Main(){ int i = 123; int count = 0; int n = i; //while loop may execute zero times. while(i > 0){ ++count; i = i/10; http://www.c-sharpcorner.com/Language/ControlStatements1.asp (3 of 6) [11/27/2001 7:20:30 PM]
  12. C# Corner: C# and .NET Developer's Network } Console.WriteLine("Number {0} contains {1} digits.",n,count); } } The above code shows simple while loop, which finds out the number of digits in a number. for a given number i = 123, the loop will execute tree times and hence the value of count will be three at the end of the while loop. The above code has one logical flaw. If the number i is set to 0, the output of the code will be “Number 0 contains 0 digits.” Actually number 0 is of one digit. Since the condition for the while loop i>0 is false from beginning for the value i=0, the while loop won’t execute any time and count will be zero. here is a solution: List: 2 DoTest.cs //find out the number of digits in a given number. using System; class DoTest { public static void Main(){ int i = 0; int count = 0; int n = i; do{ ++count; i = i/10; }while(i > 0); Console.WriteLine("Number {0} contains {1} digits.",n,count); } } The Do-While construct checks condition in the end of the loop. Thus Do-while loop will execute atleast once even though the condition to be checked is false from beginning. The For Statement //For loop with break and continue statements List: 3 Fortest.cs using System; class ForTest { public static void Main(){ for(int i = 0 ; i < 20 ; ++i){ if(i == 10) break; if(i == 5) continue; Console.WriteLine(i); } } http://www.c-sharpcorner.com/Language/ControlStatements1.asp (4 of 6) [11/27/2001 7:20:30 PM]
  13. C# Corner: C# and .NET Developer's Network } The For loop is good when we know how many times the loop needs to be executed. the output of the above code will be: 0 1 2 3 4 6 7 8 9 isn’t it self explanatory? when i becomes 5, the loop will skip over the remaining statements in the loop and go back to the post loop action. thus 5 is not part of the output. when i becomes 10, control will break out of the loop. The foreach statement This statement allows to iterate over the elements in arrays and collections. here is a simple example. List: 4 ForEach.cs //foreach loop using System; class ForEach { public static void Main(){ string[] a = {"Chirag","Bhargav","Tejas"}; foreach(string b in a) Console.WriteLine(b); } } Within the foreach loop parenthesis , the expression is made up of two parts separated by keyword in. The right hand side of in is the collection and left hand side is the variable with type identifier matching to whatever type the collection returns. Every time the collection is queried for a new value. As long as the collection returns value, the value is put into the variable and expression will return true. when the collection is fully traversed, the expression will return false and control will be transferred to the next statement after a loop. http://www.c-sharpcorner.com/Language/ControlStatements1.asp (5 of 6) [11/27/2001 7:20:30 PM]
  14. C# Corner: C# and .NET Developer's Network Enjoy Coding. About the Author: Amisha Mehta, currently working as Academic Head with STG, Ahmedabad, INDIA. More details are coming soon... contact: webmaster@c-sharpcorner.com copyright © 2000 c-sharpcorner.com. All rights are reserved. See terms and condition to use this site and its contents. Sponsors: devexpress microgold apress http://www.c-sharpcorner.com/Language/ControlStatements1.asp (6 of 6) [11/27/2001 7:20:30 PM]
  15. C# Corner: C# and .NET Developer's Network Control Statements All Source Code ADO.NET Author: Amisha Mehta Exception Handling General Control Statements – Loops GDI+ Internet This lesson shows you how to use c# looping statements. In c#, there are four iteration constructs rather than three. There Mobiling are familiar for, do, while loops and a new one from Visual basic, foreach. Multithreading Networking Printing Let us start with while loop. Security Strings & Arrays The Do and While Statements Windows Forms WebForms List: 1 WhileTest.cs XML.NET - Articles Effective C# FAQ //find out the number of digits in a given number. How do I Learn C# using System; Tutorials class WhileTest { public static void Main(){ Source Code int i = 123; Articles int count = 0; FAQ int n = i; Learn VB.NET Tutorials //while loop may execute zero times. while(i > 0){ ++count; i = i/10; Source Code } Articles FAQ Console.WriteLine("Number {0} contains {1} digits.",n,count); Learn ASP.NET Tutorials } } COBOL.NET The above code shows simple while loop, which finds out the number of digits in a number. for a given number i = 123, Eiffel.NET the loop will execute tree times and hence the value of count will be three at the end of the while loop. FoxPro.NET JScript .NET The above code has one logical flaw. If the number i is set to 0, the output of the code will be “Number 0 contains 0 Learn XML digits.” Actually number 0 is of one digit. Since the condition for the while loop i>0 is false from beginning for the value Mobile & SOAP i=0, the while loop won’t execute any time and count will be zero. here is a solution: My Services Open Source List: 2 DoTest.cs VBScript .NET VC++.NET Visual J# //find out the number of digits in a given number. Visual Studio.NET using System; class DoTest { public static void Main(){ int i = 0; int count = 0; http://www.c-sharpcorner.com/Language/ControlStatements2.asp (1 of 3) [11/27/2001 7:20:44 PM]
  16. C# Corner: C# and .NET Developer's Network About Us int n = i; Books Book Chapters do{ Career Center ++count; Discussion i = i/10; Downloads }while(i > 0); Events Hosting Console.WriteLine("Number {0} contains {1} digits.",n,count); Links Magazines Members } News Letter } Sponsors Training Tools Jobs Join C# Corner The Do-While construct checks condition in the end of the loop. Thus Do-while loop will execute atleast once even though Our Partners the condition to be checked is false from beginning. Submit Code Win Prizes The For Statement //For loop with break and continue statements Mindcracker List: 3 Fortest.cs using System; class ForTest { public static void Main(){ for(int i = 0 ; i < 20 ; ++i){ if(i == 10) break; if(i == 5) continue; Console.WriteLine(i); } } } The For loop is good when we know how many times the loop needs to be executed. the output of the above code will be: 0 1 2 3 4 6 7 8 9 isn’t it self explanatory? when i becomes 5, the loop will skip over the remaining statements in the loop and go back to the http://www.c-sharpcorner.com/Language/ControlStatements2.asp (2 of 3) [11/27/2001 7:20:44 PM]
  17. C# Corner: C# and .NET Developer's Network post loop action. thus 5 is not part of the output. when i becomes 10, control will break out of the loop. The foreach statement This statement allows to iterate over the elements in arrays and collections. here is a simple example. List: 4 ForEach.cs //foreach loop using System; class ForEach { public static void Main(){ string[] a = {"Chirag","Bhargav","Tejas"}; foreach(string b in a) Console.WriteLine(b); } } Within the foreach loop parenthesis , the expression is made up of two parts separated by keyword in. The right hand side of in is the collection and left hand side is the variable with type identifier matching to whatever type the collection returns. Every time the collection is queried for a new value. As long as the collection returns value, the value is put into the variable and expression will return true. when the collection is fully traversed, the expression will return false and control will be transferred to the next statement after a loop. Enjoy Coding. About the Author: Amisha Mehta, currently working as Academic Head with STG, Ahmedabad, INDIA. More details are coming soon... contact: webmaster@c-sharpcorner.com copyright © 2000 c-sharpcorner.com. All rights are reserved. See terms and condition to use this site and its contents. Sponsors: devexpress microgold apress http://www.c-sharpcorner.com/Language/ControlStatements2.asp (3 of 3) [11/27/2001 7:20:44 PM]
  18. C# Corner: C# and .NET Developer's Network All Source Code ADO.NET Learn C#: C# language's preprocessor directives Exception Handling Author Date of Submission User Level General GDI+ Vivek Gupta 6/5/2001 Beginner Internet Mobiling C# preprocessor is fundamentally very similar to C preprocessor and the whole concept in C# has been taken from C language Multithreading Networking specification. Printing Security “The C preprocessor is a macro processor that is used automatically by the C compiler to transform your Strings & Arrays program before actual compilation. It is called a macro processor because it allows you to define macros, Windows Forms which are brief abbreviations for longer constructs.” WebForms XML.NET But in C# only concept has been taken from C. But the C# compiler does not have a separate - preprocessor, the directives described in this C# are processed as if there was one. Unlike C and C++ Articles directives, you cannot use these directives to create macros. Effective C# FAQ How do I A preprocessor directive must be the only instruction on a line. Preprocessing directives are lines in your Learn C# program that start with `#'. Whitespace is allowed before and after the `#'. The `#' is followed by an Tutorials identifier that is the directive name. For example, `#define' is the directive The C# language's preprocessor directives are as follows Source Code #if Articles q FAQ #else q Learn VB.NET #elif Tutorials q #endif q #define q #undef Source Code q Articles #warning q FAQ Learn ASP.NET #error q Tutorials #line q #region q #endregion q COBOL.NET Eiffel.NET Main use of directives are FoxPro.NET JScript .NET 1. Conditional compilation. Using special preprocessing directives, you can include or exclude parts Learn XML of the program according to various conditions. Mobile & SOAP 2. Line control. If you use a program to combine or rearrange source files into an intermediate file, My Services which is then compiled, you can use line control to inform the compiler of where each source line Open Source originally came from. VBScript .NET VC++.NET 3. Error and Warning reporting: The directive `#error' causes the preprocessor to report a fatal Visual J# error and the directive `#warning' is like the directive `#error', but causes the preprocessor to Visual Studio.NET issue a warning and continue preprocessing. Region and Unregion is new directives. It was not in C and C++ list of directives. I don’t know the intention of C# developers to excludes number of directive from C and C++ list but they picked what people use atmost from list of C and C++ directive. Before telling meaning of each preprocessor directive I want to explain how to define preprocessor directive. There are two method to define directive. 1. Define in your C# program http://www.c-sharpcorner.com/Language/PreprocessorDirectivesCS.asp (1 of 6) [11/27/2001 7:26:05 PM]
  19. C# Corner: C# and .NET Developer's Network About Us 2. Define them at command line on compile time Books Book Chapters Here is example for first way Career Center Discussion Downloads Example Events Hosting #define TEST Links Magazines using System; Members public class MyClass News Letter { Sponsors public static void Main() Training { Tools #if (TEST) Jobs Console.WriteLine("TEST is defined"); Join C# Corner #else Our Partners Console.WriteLine("TEST is not defined"); Submit Code #endif Win Prizes } } Output Mindcracker TEST is defined In other way you can define it at command line. So program will be like this. Example using System; public class MyClass { public static void Main() { #if (TEST) Console.WriteLine("TEST is defined"); #else Console.WriteLine("TEST is not defined"); #endif } } At compile time user can define as below csc /define:TEST MyClass.java Output TEST is defined And if the command line will be like csc MyClass.java Output TEST is not defined Now its time to explain about various preprocessor define. #if directive The `#if' directive in its simplest form consists of http://www.c-sharpcorner.com/Language/PreprocessorDirectivesCS.asp (2 of 6) [11/27/2001 7:26:05 PM]
  20. C# Corner: C# and .NET Developer's Network #if expression controlled text #endif /* expression */ The comment following the `#endif' is not required, but it is a good practice because it helps people match the `#endif' to the corresponding `#if'. Such comments should always be used, except in short conditionals that are not nested. Above two example have shown how to use `#if` directive. #else directive The `#else' directive can be added to a conditional to provide alternative text to be used if the condition is false. This is what it looks like: #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */ If expression is nonzero, and thus the text-if-true is active, then `#else' acts like a failing conditional and the text-if-false is ignored. #elif directive `#elif' stands for "else if". Like `#else', it goes in the middle of a `#if'-`#endif' pair and subdivides it; it does not require a matching `#endif' of its own. Like `#if', the `#elif' directive includes an expression to be tested. The text following the `#elif' is processed only if the original `#if'-condition failed and the `#elif' condition succeeds. More than one `#elif' can go in the same `#if'-`#endif' group. Then the text after each `#elif' is processed only if the `#elif' condition succeeds after the original `#if' and any previous `#elif' directives within it have failed. `#else' is equivalent to `#elif 1', and `#else' is allowed after any number of `#elif' directives, but `#elif' may not follow `#else'. Example http://www.c-sharpcorner.com/Language/PreprocessorDirectivesCS.asp (3 of 6) [11/27/2001 7:26:05 PM]
ADSENSE
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2