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

187 455 0
C# Corner: C# and .NET Developer''''s NetworkAll Source Code ADO.NET ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials 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 Working with Data Types Explains data types in C# and how to work with them Control Statements: Part By Amisha Mehta This lesson shows how to use c# control statements, and the difference between these controls in c++/ JAVA and c# Sample includes single selection, if-else, and multi case Control Statements: Part By Amisha Mehta This lesson shows how to use while do, for, for each statements Language Preprocessor Directives By Vivek Gupta Control Statements: Part Control Statements: Part Constructors and Destructors Properties in C# Exception Handling Using Attributes in C# Properties in C#: New cover on old book by Vivek Gupta Namespaces Properties are accessor methods whose job it is to retrieve and set the values of fields 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++ What & Why? Properties: Part by Rahul Sharma If you were programming in C or C++ before coming to C#, you'll be a little bit confused about properties In the part 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 the variable Difference using Directive and Statement by Yoganand Aiyadurai This article explains difference between using directive and using statement with sample example C# and its Types by G Gnana Arun Ganesh COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET C# and its Types Encapsulation in C# What & Why? Properties: Part by Rahul Sharma Source Code Articles FAQ Learn ASP.NET Tutorials Working with Data Types This lesson contains C# language preprocessor directrives including #if, #else, #define, #undef, #line etc with sample examples Exception Handling By Amisha Mehta Source Code Articles FAQ Learn VB.NET Tutorials Introduction to C# A detailed tutorial and C# types such as Value and Reference types with sample examples Events and Delegates By Kunal Cheda Events in C# are based on delegates, the Originator defining one or more callback functions for each: A C# Syntax which Java lacks by Ashish Banerjee C# has a new iteration syntax called foreach, which I believe has been inherited from Visual Basic ( correct me if I am wrong) C# and its features by G Gnana Arun Ganesh 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] Exploring Delegates C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes Understanding and Implementing Namepsaces in C# by G Gnana Arun Ganesh Namespaces allow you to create a system to organize your code A good way to organize your namespaces is via a hierarchical system Operator Overloading in C# by Prasad H The Source code below shows how to use OperatorOverloading in C# Operator Overloading is pretty useful concept derived from C++ by C# Using Attributes in C# by Rajadurai P This article shows how to create custom attribute classes, use them in code, and query them Using Array class and IEnumeration by Rajadurai P This article illustrates the usage of Array class and IEnumerator Array class Provides methods for creating, manipulating, searching and sorting arrays, thereby serving as the base class for all arrays in the common language runtime Enumerators in C# by Prasad H Sample example shows you how to use enumerators in C# Visual Inheritance in C# by Prasad H Mindcracker 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 Visual Inheritance Part 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 Inheritance and Polymorphism Rajesh V S By using the concept of inheritance, it is possible to create a new class from an existing one and add new features to it Exploring Delegates By Filip Bulovic Delegates are a kind of type safe function pointers which are actually declared as class derived from System.MulticastDelegate BitWise Operations in C# By Chandra Hundigam 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] C# Corner: C# and NET Developer's Network Pointers in C# Rajesh V S 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 Implementing Stacks in C# Danish Sami 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 Working with Namespaces in C# Rajesh V S 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 to NET Beta This article contains C# language changes from Beta to Beta 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] 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] C# Corner: C# and NET Developer's Network Introduction to C# All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials This part of tutorial explains about C# and how to write and compile your first C# program from command line What is C#? C# is a new programming language developed by Microsoft C# has power of C++ since it's derived from C and C++ It is simpler as VB Besides that, C# is a Java like language for web programming and it has some good features of Delphi too Microsoft says, that C# is the best language to develop its NET Framework applications Installing NET SDK 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, you need to follow these steps: q q q Source Code Articles FAQ Learn VB.NET Tutorials Source Code Articles FAQ Learn ASP.NET Tutorials COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET Install IE 5.5 Install Microsoft NET Framework SDK It's free and you can download it here NET Framework SDK After installing these you can write your code in any text editor and save it as cs extension Type this in an notepad and save as my.cs C# Compiler and Editors NET SDK Beta release of Microsoft's new platform, NET, incorporated with C# command line compiler You must have to install NET SDK to run a C# program Once you install NET SDK, 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 Write your first C# program Writing your first C# program is similar to writing C++ applications You open any text editor, I described in the above paragraph and type this code using System; class MyClass { static void Main() { Console.WriteLine("Hello World!"); } } Compile your first C# program Now use command line to compile your cs file C# compiler takes at least one argument i.e., file 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] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes Now, lets take a look of your code line by line The first line of your program is using System using System Why using System? System is a namespace which stores system classes The Console class, I used in the program to display the output on the console is defined in the System namespace That's why this like is there Next line is class MyClass The class keyword in C# is used to create a new class class MyClass { } 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 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] C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials Working with Data Types First thing a programmer looks for is what kind of data types a programming languages has and how to use them In this part, I will cover C# data types and how to use them in a program Basic Data Types Most of the data type in c# are taken from C and C++ This tables lists data types, their description, and a sample example Data Type Description Example object The base type of all types object obj = null; string String type - sequence of Unicode characters string str = "Mahesh"; sbyte 8-bit signed integral type sbyte val = 12; short 16-bit signed integral type short val = 12; int 32-bit signed integral type int val = 12; long 64-bit signed integral type long val1 = 12; long val2 = 34L; bool Boolean type; a bool value is either true or false bool val1 = true; bool val2 = false; char COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET byte val1 = 12; byte val2 = 34U; ushort 16-bit unsigned integral type ushort val1 = 12; ushort val2 = 34U; uint 32-bit unsigned integral type uint val1 = 12; uint val2 = 34U; 64-bit unsigned integral type ulong ulong ulong ulong float Single-precision floating point type float val = 1.23F; double Double-precision floating point type double val1 = 1.23; double val2 = 4.56D; decimal Source Code Articles FAQ Learn ASP.NET Tutorials char val = 'h'; 8-bit unsigned integral type ulong Source Code Articles FAQ Learn VB.NET Tutorials Character type; a char value is a Unicode character byte Precise decimal type with 28 significant digits decimal val = 1.23M; val1 val2 val3 val4 = = = = 12; 34U; 56L; 78UL; Types in C# C# supports two kinds of types: value types and reference types Types Description Value Types Includes simple data types such as int, char, bool, enums Reference Types Includes object, class, interface, delegate, and array types 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] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes MyClass cls1 = new MyClass(); Data Type Conversions C# supports two types of conversions Implicit conversions and explicit conversions Implicit conversions are direct conversion For example: int iVal = 34; long lVal = intValue; Explicit conversions includes type casting conversion For example: long lVal = 123456; int iVal = (int) lVal; Mindcracker Mahesh is Admin and the founder of C# Corner He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over 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] C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials Control Statements Author: Amisha Mehta Control Statements: This lesson shows how to use c# control statements, and the difference between these controls in c++/ JAVA and c# The first selection statement is programmer’s favorite “if” statement It has three forms: Single selection if-then-else selection multi-case selection List: IfTest.cs //understanding if statement using System; class IfTest { Source Code Articles FAQ Learn VB.NET Tutorials public static void Main(){ string s; int i; Console.WriteLine("Enter a Number: "); s = Console.ReadLine(); i = Int32.Parse(s); Source Code Articles FAQ Learn ASP.NET Tutorials //single selection if(i > 0) Console.WriteLine("The number {0} is positive",i); //if-then-else selection COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET if(i > 0) Console.WriteLine("The number {0} is positive",i); else Console.WriteLine("The number {0} is not positive",i); //multi-case selection if(i == 0) Console.WriteLine("The number is zero"); else if(i > 0) Console.WriteLine("The number {0} is positive",i); 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] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes 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 There is one difference The expression in an if statement must resolve to bool value Take a look at following code list: using System; class IfTest2 { public static void Main(){ if(1) Console.WriteLine("The if statement executed"); } } when this code is complied by c# compiler, it will give the error “constant value can not be converted to bool” Mindcracker conditional or ( || ) and conditional and ( && ) operators are used in the same manner Consider the following code List: 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 % == && year % 100 != 0) || year % 400 == ) 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: SwitchTest1.cs using System; class SwitchTest1 { public static void Main(){ int i = 1; switch(i){ case : 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] C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials Source Code Articles FAQ Learn VB.NET Tutorials Source Code Articles FAQ Learn ASP.NET Tutorials COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET The out and ref parameters in C# 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 In this article, I will explain how you use these parameters in your C# applications The out Parameter The out parameter can be used to return the values in the same variable passed as a parameter of the method Any changes made to the parameter will be reflected in the variable public class mathClass { public static int TestOut(out int iVal1, out int iVal2) { iVal1 = 10; iVal2 = 20; return 0; } public static void Main() { int i, j; // variable need not be initialized Console.WriteLine(TestOut(out i, out j)); Console.WriteLine(i); Console.WriteLine(j); } } The ref parameter The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method If you any changes to the variable, they will be reflected in the variable You can even use ref for more than one method parameters http://www.c-sharpcorner.com/Language/out_and_ref.asp (1 of 2) [11/27/2001 8:24:44 PM] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes Mindcracker namespace TestRefP { using System; public class myClass { public static void RefTest(ref int iVal1 ) { iVal1 += 2; } public static void Main() { int i; // variable need not be initialized i = 3; RefTest(ref i ); Console.WriteLine(i); } } } About the Author: Mahesh is Admin and the founder of this site He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over years 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/out_and_ref.asp (2 of 2) [11/27/2001 8:24:44 PM] C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials Serializing Objects in C# Author Date of Submission User Level Bipin Joshi 05/14/2001 Intermediate Introduction In simple words serialization is a process of storing the object instance to a disk file Serialization stores state of the object i.e member variable values to disk Deserialization is reverse of serialization i.e it's a process of reading objects from a file where they have been stored In this code sample we will see how to serialize and deserialize objects using C# Namespaces involved Following namespaces are involved in serialization process : q System.Runtime.Serialization q System.Runtime.Serialization.Formatters.Binary Example Source Code Articles FAQ Learn VB.NET Tutorials using using using using System; System.IO; System.Runtime.Serialization; System.Runtime.Serialization.Formatters.Binary; public class SerialTest { Source Code Articles FAQ Learn ASP.NET Tutorials COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET public void SerializeNow() { ClassToSerialize c=new ClassToSerialize(); File f=new File("temp.dat"); Stream s=f.Open(FileMode.Create); BinaryFormatter b=new BinaryFormatter(); b.Serialize(s,c); s.Close(); } public void DeSerializeNow() { ClassToSerialize c=new ClassToSerialize(); File f=new File("temp.dat"); Stream s=f.Open(FileMode.Open); BinaryFormatter b=new BinaryFormatter(); c=(ClassToSerialize)b.Deserialize(s); Console.WriteLine(c.name); s.Close(); } public static void Main(string[] s) { SerialTest st=new SerialTest(); st.SerializeNow(); st.DeSerializeNow(); } http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp (1 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes } public class ClassToSerialize { public int age=100; public string name="bipin"; } Explanation Here we have our own class named ClassToSerialize This class has two public valiables name and age with some default values We will write this class to a disk file (temp.dat) using SerializeTest class SerializeTest class has two methods SerializeNow() and DeSerializeNow() which perform the task of serialization and deserialization respectively The general steps for serializing are : Mindcracker q Create an instance of File that will store serialized object q Create a stream from the file object q Create an instance of BinaryFormatter q Call serialize method of the instance passing it stream and object to serialize The steps for de-serializing the object are similar The only change is that you need to call deserialize method of BinaryFormatter object Now, let us see an example where we have used 'real' class with public and shared members and properties to encapsulate them The class also uses another supporting class This is just to make clear that if your class contains further classes, all the classes in the chain will be serialized Example using using using using System; System.IO; System.Runtime.Serialization; System.Runtime.Serialization.Formatters.Binary; public class SerialTest { public void SerializeNow() { ClassToSerialize c=new ClassToSerialize(); c.Name="bipin"; c.Age=26; ClassToSerialize.CompanyName="xyz"; File f=new File("temp.dat"); Stream s=f.Open(FileMode.Create); BinaryFormatter b=new BinaryFormatter(); b.Serialize(s,c); s.Close(); } public void DeSerializeNow() { ClassToSerialize c=new ClassToSerialize(); File f=new File("temp.dat"); http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp (2 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network } Stream s=f.Open(FileMode.Open); BinaryFormatter b=new BinaryFormatter(); c=(ClassToSerialize)b.Deserialize(s); Console.WriteLine("Name :" + c.Name); Console.WriteLine("Age :" + c.Age); Console.WriteLine("Company Name :" + ClassToSerialize.CompanyName); Console.WriteLine("Company Name :" + c.GetSupportClassString()); s.Close(); public static void Main(string[] s) { SerialTest st=new SerialTest(); st.SerializeNow(); st.DeSerializeNow(); } } public class ClassToSerialize { private int age; private string name; static string companyname; SupportClass supp=new SupportClass(); public ClassToSerialize() { supp.SupportClassString="In support class"; } public int Age { get { return age; } set { age=value; } } public string Name { get { return name; } set { name=value; } } public static string CompanyName { get { return companyname; } set { companyname=value; } } http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp (3 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network public string GetSupportClassString() { return supp.SupportClassString; } } public class SupportClass { public string SupportClassString; } Example The final example shows how to serialize array of objects using using using using System; System.IO; System.Runtime.Serialization; System.Runtime.Serialization.Formatters.Binary; public class SerialTest { public void SerializeNow() { ClassToSerialize[] c=new ClassToSerialize[3]; c[0]=new ClassToSerialize(); c[0].Name="bipin"; c[0].Age=26; c[1]=new ClassToSerialize(); c[1].Name="abc"; c[1].Age=75; c[2]=new ClassToSerialize(); c[2].Name="pqr"; c[2].Age=50; } ClassToSerialize.CompanyName="xyz"; File f=new File("temp.dat"); Stream s=f.Open(FileMode.Create); BinaryFormatter b=new BinaryFormatter(); b.Serialize(s,c); s.Close(); public void DeSerializeNow() { ClassToSerialize[] c; File f=new File("temp.dat"); Stream s=f.Open(FileMode.Open); BinaryFormatter b=new BinaryFormatter(); c=(ClassToSerialize[])b.Deserialize(s); Console.WriteLine("Name :" + c[2].Name); Console.WriteLine("Age :" + c[2].Age); Console.WriteLine("Company Name :" + ClassToSerialize.CompanyName); s.Close(); } public static void Main(string[] s) { http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp (4 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network SerialTest st=new SerialTest(); st.SerializeNow(); st.DeSerializeNow(); } } public class ClassToSerialize { private int age; private string name; static string companyname; public int Age { get { return age; } set { age=value; } } public string Name { get { return name; } set { name=value; } } public static string CompanyName { get { return companyname; } set { companyname=value; } } } About the author Bipin Joshi is a programmer working in Mumbai(India) His personal web site at www.bipinjoshi.com provides lot of stuff related to NET He also contributes to other web sites in the form of articles and source code He can be reached at bipinjoshi@yahoo.com contact: webmaster@c-sharpcorner.com http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp (5 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network 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/serializingObjectsinCS.asp (6 of 6) [11/27/2001 8:24:52 PM] C# Corner: C# and NET Developer's Network All Source Code ADO.NET Exception Handling General GDI+ Internet Mobiling Multithreading Networking Printing Security Strings & Arrays Windows Forms WebForms XML.NET Articles Effective C# FAQ How I Learn C# Tutorials Source Code Articles FAQ Learn VB.NET Tutorials Source Code Articles FAQ Learn ASP.NET Tutorials COBOL.NET Eiffel.NET FoxPro.NET JScript NET Learn XML Mobile & SOAP My Services Open Source VBScript NET VC++.NET Visual J# Visual Studio.NET Operator Overloading 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 using System; class Rectangle { private int iHeight; private int iWidth; public Rectangle() { Height=0; Width=0; } public Rectangle(int w,int h) { Width=w; Height=h; } public int Width { get { return iWidth; } set { iWidth=value; } } public int Height { get { return iHeight; } set { iHeight=value; } } public int Area { get { return Height*Width; } } /* OverLoading == */ public static bool operator==(Rectangle a,Rectangle b) { return ((a.Height==b.Height)&&(a.Width==b.Width)); } /* OverLoading != */ http://www.c-sharpcorner.com/Language/OperatorOverloadingSample1.asp (1 of 3) [11/27/2001 8:24:56 PM] C# Corner: C# and NET Developer's Network About Us Books Book Chapters Career Center Discussion Downloads Events Hosting Links Magazines Members News Letter Sponsors Training Tools Jobs Join C# Corner Our Partners Submit Code Win Prizes Mindcracker public static bool operator!=(Rectangle a,Rectangle b) { return !(a==b); } /* Overloding > */ public static bool operator>(Rectangle a,Rectangle b) { return a.Area>b.Area; } /* Overloading < */ public static bool operatorb); } /* Overloading >= */ public static bool operator>=(Rectangle a,Rectangle b) { return (a>b)||(a==b); } /* Overloading

Ngày đăng: 12/08/2014, 06:20

Từ khóa liên quan

Mục lục

  • c-sharpcorner.com

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

    • C# Corner: C# and .NET Developer's Network

Tài liệu cùng người dùng

Tài liệu liên quan