Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 11000 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > A New Keyword Overview ...
Sabarimahesh

A New Keyword Overview

 Blog author: Sabarimahesh | Posted on: 5/19/2012 | Category: C# Blogs | Views: 728 | Status: [Member] | Points: 75 | Alert Moderator   


Introduction

Through this topic I'm going to share about new KeyWord in C#
I believe that this will be very much helpful for beginners

new


  • new is nothing but an Operator Keyword
  • new can be classified into three types.

      They are:

1)      new Operator

2)      new Modifier

3)      new Constraint



New Operator


  • new Operator is used to create objects and Call on constructors

    And also create instances for anonymous types

 

Example 1


To Create an Object
ClassA  Cat= new ClassA ();
ClassA Rat=new ClassA();


Here,Cat and Rat are Objects of ClassA



Example 2


To Create instance for anonymous types
var Cat = new { Dotnet= "Rat" }
Console.WriteLine(Cat.Dotnet );


Here,Cat is an object for anonymous


Example 3


To Call on Default Constructor
string str = new string ();


Here,str is an object.Once it is created, default constructor is called 



new Modifier

  • Hides a member inherited from a base class.
  • i.e., Derived-Class me replaces the Base-Class.
  • We can hide without the use of the new modifier, a warning will be generated.
  • If we need to avoid the warning, the new modifier is a must.
  • The New Modifier  will hide in  class or struct the following items  from all base class members with the same name

1)      Constant

2)      Field

3)      Property

4)      Method

5)      Indexer


Example 1


Class BaseClass consists of function Show().

Class DerivedClass hides the function Show()

class Program
    {             static void Main(string[] args)
        {
         Baseclass bc;
         bc = new Baseclass();
         bc.Show();
         Console.ReadLine();
 
        }
  
    }
 
    class Baseclass
    {
        public void Show()
        {
            System.Console.WriteLine("Baseclass::Show");
        }
    }
 
    class DerivedClass : Baseclass
    {
        new public void Show()
        {
            System.Console.WriteLine("DerivedClass::Show");
        }
    }
 

Output


Baseclass::Show

Show() of class BaseClass is executed because the reference variable b refers to the object of class BaseClass.


new Constraint


  • Any type argument in a generic class declaration should have a public parameter less constructor.
  • Apply the new constraint to a type parameter when the generic class creates new instances of the same type.

Example 1
class ItemFactory<T> where T
: new()
{
 public T GetNewItem()
 {
return new T();
}
}
Example 2
public class ItemFactory<T> where T : IComparable, new()
{
}

Conclusion


We have learned about new Keyword. I hope that my article was helpful. 





Life is a Race
Thanks & Regards
By
Sabari Mahesh P M
Found interesting? Add this to:


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, March 15, 2012
Level:Bronze
Status: [Member]
Biography:C#,ASP.NET, JAVA SCRIPT,SQL, J QUERY,MVC 3
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 5:50:43 PM