简体   繁体   English

剃刀上的ViewBag转换成本-C#MVC

[英]ViewBag conversion cost on razor - c# mvc

Has someone tried to measure the conversion times of using ViewBag object directly on a comparison to the original object class? 是否有人试图直接将ViewBag对象与原始对象类进行比较来衡量转换时间? Here is an example of what I am talking about. 这是我在说的一个例子。 Say I have a class named Client: 假设我有一个名为Client的类:

public class Client{
    public int Id { get; set; }
    public string Name { get; set; }
}

On a razor file I could do one of the following: 在剃刀文件上,我可以执行以下任一操作:

var client = (Client)ViewBag.Client;
var name = client.Name;

Or 要么

var name = (Client)ViewBag.Client.Name;

I was wondering about the cost of the operation directly on the ViewBag object (without the conversion) comparing to the object on its original class. 我想知道与原始类上的对象相比,直接在ViewBag对象上进行操作的成本(不进行转换)。 Is the performance, when having multiples calls on ViewBag, an issue? 在ViewBag上进行多次调用时,性能是否存在问题?

Well your first example will create 2 variables in your memory. 好吧,您的第一个示例将在您的内存中创建2个变量。 The second example will create 1 variable in your memory. 第二个示例将在您的内存中创建1个变量。

For the rest I think there is no real performance difference between the two. 对于其余部分,我认为两者之间没有真正的性能差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM