I am really confused about NEW keyword use in C# and MVC

时间:2015-06-26 10:04:42

标签: c#

What I know about new are as follows 1) when creating object in class ,array list etc for example:

public class Employee {
public int ID {get;set;}
Public String Name{get;set;}
}


}
public static main(){
var emp n = new Employee;

}

2) Modifier

public class BaseC
{
    public static int x = 55;
    public static int y = 22;
}

public class DerivedC : BaseC
{
    // Hide field 'x'. 
    new public static int x = 100;

    static void Main()
    {
        // Display the new value of x:
        Console.WriteLine(x);

        // Display the hidden value of x:
        Console.WriteLine(BaseC.x);

        // Display the unhidden member y:
        Console.WriteLine(y);
    }
}
/*
Output:
100
55
22
*/

but I am going to show some scenario where I feel difficult how the new key word is implementing here . I think first new word is used to create manager object but what is the use of second new or (new UserStore<ApplicationUser>. Ccould you explain it please.

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

how is the new keyword used to invoke this statement below:

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

6 个答案:

答案 0 :(得分:0)

The 'new' keyword in c# is used for following reasons:

  1. You want to allocate memory (create object) in managed heap for your new object

  2. You want to replace method in base class with new method of same name

  3. Constraints of Generic types (it means that object will be created INSIDE generic method)

Examples :

  1. A instance = new A();

  2. class A { public void MyMethod() {} }

    class B : A { public new void MyMethod() {} }

  3. Class C where MyType : new() { T instance = new T(); }

答案 1 :(得分:0)

Mentally translate the "new" used within a member declaration to "hide-base-class-member".

答案 2 :(得分:0)

The first example is used to construct a new instance of an object.

The second example is used to replace a field when defining a subclass.

Fields can be accessed by subclasses, so when you try and access the field 'x', it's always the one from the base class.

But what if you have a real need for the field x to refer to something different in the subclass? Putting the new modifier means that a unqualified usage of x refers to the x field in this class, not the superclass (you can use base.x to refer to that one).

This main use case for this version of the new keyword is method declarations, where it comes in handy for interface implementations, and especially handy for multiple interface implementations.

You can have a method X() defined on the interface, and another method new X() on the implementation class, and the right method will get called depending on the type of the reference... casting from an interface to the implementation class will cause a different method to be called.

答案 3 :(得分:0)

In very short: new [Classname] calls a Constructor of a Class (e.g. your new Employee) and creates an object in an allocated memory space, new in front of a variable/method declaration (e.g. new public static int x = 100;) is used to "hide" a previous declaration.

The Syntax for new Employee is wrong:

var emp n = new Employee;

should be

var emp = new Employee();

var: type of the variable (that is a variable type the compiler detects but could also be Employee)

emp: the name of the varibale pointing to the instance

Employee(): The class name and the () for the empty constructor to call (this is created by default if you don't have specified one.)

答案 4 :(得分:0)

According to the MSDN reference:

In C#, the new keyword can be used as an operator, a modifier, or a constraint.

new as an Operator:

    Used to create objects and invoke constructors.

new as a Modifier:

    Used to hide an inherited member from a base class member.

new as a Constraint:

   Used to restrict types that might be used as arguments for a type 
   parameter in a generic declaration.

In the line:

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

You are creating a "new" ApplicationUserManager

答案 5 :(得分:0)

实际问题是如何

ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

可以解释,对吧?

非常简单:

您创建新的特定类型T的用户区,其中 T 是应用程序用户

所以

UserStore<ApplicationUser> myStore = new UserStore<ApplicationUser>();

这正是你所说的1),除了通用的“T”之外没什么特别的。 在您的特定情况下,我们使用重载,我们使用带有上下文对象的构造函数

UserStore<ApplicationUser> myStore = new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>());

之后你在你的应用程序管理器中使用这个新创建的对象(尽管我认为你的例子遗漏了一些东西)

ApplicationUserManager(myStore);

上面的初始代码是一个简单的单行代码,因为你不需要myStore对象。