Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my own datatype in C# program?

Tags:

c#

types

Like "int" refers to "Int32" class, "string" refers to "String" class. How to refer a datatype like "abc" to my "Abc" class?

like image 737
chanchal1987 Avatar asked Jul 19 '10 10:07

chanchal1987


People also ask

How can we create our own data type?

You can create a new class which has specific fields. If you need exact size of fields you can use byte arrays. class Data { public byte[] dataA = new byte[1]; public byte[] dataB = new byte[2]; public byte[] dataC = new byte[7]; ... }

Is it possible to create your own user data type?

To create a user-defined data type. In Object Explorer, expand Databases, expand a database, expand Programmability, expand Types, right-click User-Defined Data Types, and then click New User-Defined Data Type.

How many ways are there to create custom data type in C programming?

ANSI C provides three types of data types: Primary(Built-in) Data Types: void , int , char , double , and float . Derived Data Types: Array, References, and Pointers. User Defined Data Types: Structure, Union, and Enumeration.


2 Answers

Your "class" is a data type.

The examples you give are the difference between CLR data type names and C# datatype names. They are aliases. C# int maps to CLR Int32 and C# string maps to CLR String.

You can create your own aliases by using "using Xyx=Abc". You must do this in each source file, so it is not that useful.

like image 102
Philip Smith Avatar answered Sep 19 '22 23:09

Philip Smith


You can add an alias like this:

using abc = MyNamespace.Abc;

But I would question why you would want to do this.

[Another poster pointed out a valid use, namely namespace type clashes, but then I would always use the fully qualified type name otherwise it might get very confusing.]

like image 34
Mitch Wheat Avatar answered Sep 20 '22 23:09

Mitch Wheat