Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User defined data types c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 471
    Comment on it

    Data types can be used as return data type or datatype. The syntax can include any C# data type as well as user defined data types. It can be an array or object. Or even it doesn't need to return a value. We can define and use STRUCTURE as data type when needed to combine many different types into one. This can be used when non of the elementary data type is able to serve our purpose. The combination of default value of each member in structure combines to form the default value of structure.

    SYNTAX: Structure [name] { } Example:

       struct Employee
              {
                public string EmpName;
                public String EmpAddress;
                public Int EmpId;
              }
    

    //implementation

    static void Main(string[] args)
          {
            //Creating the variable of employee type
            Employee employeeObj;
    
            // Storing value in variable
            Console.Write("Enter Employee name:\t");
            employeeObj.EmpName = Console.ReadLine();
            Console.Write("Enter Employee Address:\t");
            employeeObj.EmpAddress =  Console.ReadLine();
            Console.Write("Enter Employee ID:\t");
            employeeObj.EmpId =Convert.ToInt32(Console.ReadLine());
           //Use the variable
           Console.WriteLine("Employee Name:\t" + employeeObj.EmpName );
           Console.WriteLine("Employee Address :\t" + employeeObj.EmpAddress );
           Console.WriteLine("Employee ID\t" + employeeObj.EmpID);
        }
    

    creating properties: Structures should be immutable.

    public struct MyStruct
    {
        public readonly double Value1;
        public readonly decimal Value2;
    
        public MyStruct(double value1, decimal value2)
        {
            this.Value1 = value1;
            this.Value2 = value2;
        }
    }
    class Test
    {
        private MyStruct myStruct;
    
        public Test()
        {
            myStruct = new MyStruct(10, 42);
        }
    
        public MyStruct MyStruct
        {
            get { return myStruct; }
            set { myStruct = value; }
        }
    }
    

    Keep in mind:

    1. All structures inherit from System.ValueType class.
    2.Structure data types have no literal type character or identifier type character.(http://msdn.microsoft.com/en-us/library/s9cz43ek.aspx)
    3.There is no automatic conversion between to or from structure data type.
    4.VB User defined data types are not compatible with components of other languages except .net
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: