explicit and implicit c#

前端 未结 7 1941
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 13:31

I\'m new to C# and learning new words. I find it difficult to understand what\'s the meaning of these two words when it comes to programming c#. I looked in the dictionary

相关标签:
7条回答
  • 2020-12-07 13:37

    In general

    • Implicit: something is being done for you automatically.
    • Explicit: you've written something in the source code to indicate what you want to happen.

    For example:

    int x = 10;
    long y = x; // Implicit conversion from int to long
    int z = (int) y; // Explicit conversion from long to int
    

    Implicit and explicit are used quite a lot in different contexts, but the general meaning will always be along those lines.

    Note that occasionally the two can come together. For instance:

    int x = 10;
    long y = (long) x; // Explicit use of implicit conversion!
    

    (An explicit conversion is one which has to be stated explicitly; an implicit version is one which can be used implicitly, i.e. without the code having to state it.)

    0 讨论(0)
  • 2020-12-07 13:42

    I think this link makes it pretty clear what an implicit conversion is - it's one where you don't need to explicitly cast the value in an assignment. So, instead of doing

    myDigit = (Digit) myDouble 
    

    ...you can just do:

    myDigit = myDouble;
    
    0 讨论(0)
  • 2020-12-07 13:42

    Because C# is statically-typed at compile time.

    Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

    Explicit conversions (casts): Explicit conversions require a cast expression. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public static explicit operator Person(Employe employe) => new Person { Id = employe.Id, Name = employe.Name };
    }
    
    public class Employe
    {
    
        public int Id { get; set; }
        public string Name { get; set; }
        public string Family { get; set; }
    
        public static implicit operator Employe(Person person) => new Employe { Id = person.Id, Name = person.Name };
    }
    
    static void Main(string[] args)
    {
        Person person = new Person() { Id = 1, Name = "Reza" };
    
        //implicit operator
        Employe employe = person;
        employe.Family = "Jenabi";
    
        //explicit operator
        Person person1 = (Person)employe;
    }
    
    0 讨论(0)
  • 2020-12-07 13:48

    Being explicit in C# is mainly about showing your intentions clearly and unambiguously.

    For example:

    class MyClass
    {
        string myField;
    
        void MyMethod(int someNumber)
        {
    
        }
    }
    

    In the above code the visibility of the class, field and method are all implied. They use the compiler defaults.

    Now, I can never remember what the compiler defaults are, and maybe your colleagues can't either, so rather than rely on everyone remembering what the defaults are, you can be explicit.

    public class MyClass
    {
        private string myField;
    
        public void MyMethod(int someNumber)
        {
        }
    }
    
    0 讨论(0)
  • 2020-12-07 13:50

    The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say that you have the following class:

    public class Role
    {
        public string Name { get; set; }
    }
    

    If you want to create a new Role and assign a Name to it, you will typically do it like this:

    Role role = new Role();
    role.Name = "RoleName";
    

    Since it has only one property, it would perhaps be convenient if we could instead do it like this:

    Role role = "RoleName";
    

    This means that we want to implicitly convert a string to a Role (since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:

    public static implicit operator Role(string roleName)
    {
        return new Role() { Name = roleName };
    }
    

    Another option is to implement an explicit conversion operator:

    public static explicit operator Role(string roleName)
    {
        return new Role() { Name = roleName };
    }
    

    In this case, we cannot implicitly convert a string to a Role, but we need to cast it in our code:

    Role r = (Role)"RoleName";
    
    0 讨论(0)
  • 2020-12-07 13:55

    Implicit can be taken as implied, but explicit means that you state it must be done yourself. Like with casts. Here is an implicit cast:

    int implicit;
    implicit = 7.5;
    

    The value '7.5' will implicitly be cast as an int. This means the compiler does it for you.

    Here is explicit:

    int explicit;
    explicit = (int)7.5;
    

    Here you tell the compiler that you want it cast. You explicitly declare the conversion. Hope that helps. Source: http://cboard.cprogramming.com/cplusplus-programming/24371-implicit-explicit.html

    0 讨论(0)
提交回复
热议问题