Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

New features in C# 2.0 | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » New features in C# 2.0

New features in C# 2.0








Article Posted On Date : Thursday, March 22, 2012


New features in C# 2.0
Advertisements

Introduction

In this article, I will learn about and use the new features in C# 2.0 added by the Microsoft designers, such as generics, anonymous methods, partial types, static classes, nullable types, and limiting access to properties.

Generics

In .NET 1.x, all of the collections were declared to hold instances of System.Object, so it's not type-safe. An even bigger problem, it's when you add different types, including to boxed values, to the same collection, and later you need to access the objects, you should keep a record of the type for every object in the collection.

In .NET 2.0, it is eliminated this kind of problems using a new library of strongly type collections (System.Collections.Generic namespace) and a new C# language construction for expressing and declaring semantically generic objects, so the programmer could find type-safe bugs in compile time rather than in runtime time.

A generic provides a way for the developers to define type parameters for methods arguments and type definitions.

Let's define a custom generic class. Just add the angle brackets and the type parameters inside them to the class definition, and declare generic attributes inside the classes.

See an example below.

Let's define the concepts for a generic linked list using generic nodes.

public class Node<T>

{

        private T m_objGenericObject;

        private Node<T> m_objNext;

        //Ctor

        public Node(T objGenericObject)

        {

            this.m_objGenericObject = objGenericObject;

        }

        // Properties

        public T Data

        {

            get

            {

                return this.m_objGenericObject;

            }

        }

        //Methods

        public Node<T> Next

        {

            get

            {

                return this.m_objNext;

            }

        }

        public void Append(Node<T> objNewNode)

        {

            if (this.m_objNext== null)

            {

                this.m_objNext = objNewNode;

            }

            else

            {

                this.m_objNext.Append(objNewNode);

            }

         }

}

Listing 1.

The above definition could be read as Node of type T. Now it's possible to create a deal of linked list,

Node<String> objNode=new Node<string>("John Charles");

Node<String> objNext=new Node<string>("olamendy");

objNode.Append(objNext);

Node<int> objNode=new Node<int>(3);

Node<int> objNext=new Node<int>(4);

objNode.Append(objNext);

Listing 2.

Let's a set of generic collection classes defined by.NET designers and the non-generic counterpart. It's possible to create collections of any type that the compiler recognizes.

Generic class     Non-generic counterpart     Meaning
Collection<T>     CollectionBase     The basis for the collections
Comparer<T>     Comparer     Compares two objects for equality
Dictionary<K,V>     Hashtable     A collection of name-value pair
List<T>     ArrayList     A dynamic resizable list of items
Queue<T>     Queue     FIFO list
Stack<T>     Stack     LILO list

List<string> arrString = new List<string>();

arrString.Add("john charles");

List<int> arrInt=new List<int>();

//No boxing!

arrInt.Add(1);

//No unboxing!

int nValue = arrInt[0];

Listing 3.

Anonymous methods

Anonymous methods allow you to define method blocks inline. In general, you can use anonymous methods anywhere you can use a delegate. This can greatly simplify registering event handlers.

For registering the delegate which invokes the method, we used to write the following code.

class Program

{

        static void Main(string[] args)

        {

            SomeType t = new SomeType();

            t.SomeEvent += new SomeDelegate(MyEventHandler);

        }

        // Typically only called by the SomeDelegate object.

        public static void MyEventHandler(object Sender, EventArgs ex)

        {

           //Code implementation

        }

}

Listing 4.

Now you may write the same business logic as follow,

class Program

{

        static void Main(string[] args)

        {

            SomeType t = new SomeType();

            t.SomeEvent += new delegate(object Sender, EventArgs ex)

            {

                //Code implementation

            }

        }

}

Listing 5.

Partial Types

In previous versions of C# the entire definition for a class had to be in a single file. Now, using the partial keyword, you can split your class across more than one file. It allows to have different team members working on different parts of the class, and IDE such as Visual Studio 2005 can separate the designer-generated code from your own user code.

There are some features about partial types, such as:

    All partial type definitions must be modified with the partial keyword and must belong to the same namespace and the same module and assembly.
    The partial modifier can appear only before the class, interface, and struct keywords.
    Access modifiers (public, private, etc.) must match all the partial types of the same class.

Static classes

The purpose of a static class is to provide a set of static utility methods scoped to the name of the class. When a class has been defined as static, it is not creatable using the new keyword, and it can contain only static members or fields (if this is not the case, you receive compiler errors).

This type of construction is very useful for defining utility classes.

Let's define an utility class for converting types to System.String type.

public static class StringUtility

{

        public static string Convert(int nValue)

        {}

        public static string Convert(DateTime dtValue)

        {}

        public static string Convert(float ftValue)

        {}

}

Listing 6.

Nullable Types

With new nullable types, you can assign value types a null value. This can be tremendously powerful, especially when working with databases where the value returned might be null; without nullable types you would have no way to express that an integer value is null, or that a Boolean is neither true nor false.

Semantically when a value is null, it means that no object references exists to this alias (value name). In order to follow the principles of .NET where all entities are object (String, int, float, Person class), it was previously impossible to express that there is no object associated to an alias integer.

You can declare a nullable type as follows: System.Nullable<int> nAge; or, you may use simplified form int? nHeight.

You can check whether a nullable variable is null in two ways as well. You can check like this: if(nHeight.HasValue) or like this: if(nHeight!= null).

To see the actual value of the variable use the following construction nHeight.Value.

Limit Access Within Properties

It is now possible to restrict the accessibility level of the get and set accessors within a property using access modifiers. Usually you would restrict access to the set accessor and make the get accessor public.

In the following listing, it's granted permission to everyone for reading Customer.Name and Customer.ContactInfo properties, and it's granted permission to everyone for changing only Customer.ContactInfo property. But it's granted permission only to friend entities for changing Customer.Name property.

public class Customer

{

        private string m_strName;

        private string m_strContactInfo=null;

        public Customer(string strName)

        {

            this.m_strName=strName;

        }

        public string Name

        {

            get

            {

                return this.m_strName;

            }

            protected set

            {

                this.m_strName=value;

            }

        }

        public string ContactInfo

        {

            get

            {

                return this.m_strContactInfo;

            }

            set

            {

                this.m_strContactInfo = value;

            }

        }

}

Listing 7.






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.