简体   繁体   English

在 C# 中创建对象时如何使用 if 语句?

[英]How can i have an if statement when creating an object in c#?

I want something like this when creating an object,创建对象时我想要这样的东西,

            var modelObj = new TestModel
            {
                Id = id,
                Description = obj.Description,
                if (status == 1)
                {
                   Service = obj.Url,
                   Username = obj.ServiceUsername,
                }
                else
                {
                  Password = obj.Password,
                  Token = obj.Token,
                  FirstName = obj.FirstName
                }
            }

Is this possible to do with the if statement in c# ?这可能与 c# 中的 if 语句有关吗?

Is this possible to do with the if statement in c# ?这可能与 c# 中的 if 语句有关吗?

You can do the same thing with expressions using the conditional operator.您可以使用条件运算符对表达式执行相同的操作。

    var modelObj = new TestModel
    {
        Id = id,
        Description = obj.Description,
        Service = status == 1 ? obj.Url : null,
        Username = status == 1 ? obj.ServiceUsername : null,
        Password = status != 1 ? obj.Password : null,
        Token = status != 1 ? obj.Token : null,
        FirstName = status != 1 ? obj.FirstName : null,

    };

You can just do this afterwards if this is an object data assignment.如果这是对象数据分配,您可以在之后执行此操作。

    var modelObj = new TestModel
    {
        Id = id,
        Description = obj.Description            
    }

    if (status == 1)
    {
       modelObj.Service = obj.Url,
       modelObj.Username = obj.ServiceUsername,
    }
    else
    {
      modelObj.Password = obj.Password,
      modelObj.Token = obj.Token,
      modelObj.FirstName = obj.FirstName
    }

Otherwise best bet is to use a class constructor instead of inline initialization.否则最好的办法是使用类构造函数而不是内联初始化。

If I suggest proper way to do it then.如果我建议正确的方法,那么。

Here in the question there is no information about type of obj.在这个问题中,没有关于 obj 类型的信息。 So I am putting that as markup Obj but that needs to replace with actual type.所以我把它作为标记 Obj 但需要用实际类型替换。

  1. Create method something like this.创建类似这样的方法。
    // Here you can use specific type instead of dynamic but your code miss that.
    public TestModel GetTestModel(int status, int id, Obj obj)
    {
        if (status == 1)
        {
            return new TestModel()
                {
                    Id = id,
                    Description = obj.Description,
                    Service = obj.Url,
                    Username = obj.ServiceUsername,
                };
        }
        else
        {
            return new TestModel()
                {
                    Id = id,
                    Description = obj.Description,
                    Password = obj.Password,
                    Token = obj.Token,
                    FirstName = obj.FirstName
                };
        }
    }

Here's a version of dotnetstep's answer (that's I'd be happy if he used, and then I'd delete this answer).这是dotnetstep答案的一个版本(如果他使用我会很高兴,然后我会删除这个答案)。 I like it as it is really quite clean and easy to read.我喜欢它,因为它非常干净且易于阅读。

public TestModel CreateTestModel(int status, int id, Obj obj) =>
    status == 1
    ? new TestModel()
    {
        Id = id,
        Description = obj.Description,
        Service = obj.Url,
        Username = obj.ServiceUsername,
    }
    : new TestModel()
    {
        Id = id,
        Description = obj.Description,
        Password = obj.Password,
        Token = obj.Token,
        FirstName = obj.FirstName
    };

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

相关问题 使用c#,当我具有对象类型的变量时,如何利用可为空的值类型的好处? - Using c#, How can I take benefit of using nullable valuetypes when I have a variable of object type? 当其中之一具有join语句时,如何将c#Lambda表达式串在一起 - How do I string together c# Lambda expressions when I have a join statement in one of them 创建对象实例后,C#(wpf)无法重用 - C# (wpf) after creating object instance i can not reuse it 如何使用 C# 检查对象(我必须插入到列表中)是否已经在列表中? - How can I check if an object (that I have to insert in a list) is already in the list using C#? C#如何在类型为List时设置PropertyInfo值<T>我有一个清单<object> - C# How to set PropertyInfo value when its type is a List<T> and I have a List<object> C# - 如何使用引用实现一组接口的任何对象的类型? - C# - How can I have an type that references any object which implements a set of Interfaces? 在 C# 中,当我使用 object 作为参数时,如何在 object 上执行方法? - In C# how can I execute a method on an object when I am using that object as a parameter? 如何在C#中填充对象? - How can I fill the object in C#? 如何在 C# 中重用 object? - How can I reuse an object in C#? 我们可以用C#中的对象替换if语句吗? - Can we replace if statement with an object in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM