palabra clave char en C#

Las palabras clave son las palabras en un idioma que se utilizan para algún proceso interno o representan algunas acciones predefinidas. char es una palabra clave que se usa para declarar una variable que almacena un valor de carácter desde el rango de +U0000 a U+FFFF . Es un alias de System.Char .

Sintaxis:

char variable_name = value;

La palabra clave char ocupa 2 bytes (16 bits) en la memoria.

Ejemplo:

Input: S

Output: chr: S
        Size of a char variable: 2 

Input: G

Output: Type of chr: System.Char
        chr: G
        Size of a char variable: 2

Ejemplo 1:

// C# program for char keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'S';
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print the size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
    }
}

Producción:

chr: S
Size of a char variable: 2

Ejemplo 2:

// C# program for char keyword
using System;
using System.Text;
  
namespace geeks {
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'G';
  
        // to print the type of variable
        Console.WriteLine("Type of chr: " + chr.GetType());
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

Producción:

Type of chr: System.Char
chr: G
Size of a char variable: 2

Ejemplo 3:

// C# program for char keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'Geeks';
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
    }
}

Error:

Demasiados caracteres en carácter literal

Publicación traducida automáticamente

Artículo escrito por shivanisinghss2110 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *