I'm trying to create a struct for an 8 bit signed integer, seeing as how I'm creating a calculator it would be useful. Windows' calc.exe has this set up as precision for Qword, Dword, Word, and Bytes. I have 64, 32, and 16 because they're already programmed into Visual C#. I need information on how to build structs so that they can be assigned values such as:

private Int8 Input8Bit = 0;

Using a struct like:

public struct Int8
{

}

But I don't even know where to begin. As I stated, any help is appreciated.

I'm trying to create a struct for an 8 bit signed integer,
I need information on how to build structs so that they can be assigned values such as:

private Int8 Input8Bit = 0;

Using a struct like:

public struct Int8
{

}

But I don't even know where to begin. As I stated, any help is appreciated.

If you just need a signed 8-bit data type, try sbyte.

Well I'd still also like to learn about structs, I figured that much out a few hours after posting. I believe it's:

long: 64
int: 32
short: 16
byte: 8

Check out these two links:

Structs Tutorial
Objects, Classes and Structs

Think of a struct as a record of data. Basically a struct is similar to a class in that it groups together a bunch of data and methods that can be used by an instance. The two main differences are:

1. An instance of a struct is a value type while an instance of a class is a reference type. A struct is created on the stack while a class is created on the heap.
2. Classes can inherit from a base class while structs cannot.

I hope this helps your understanding, please re-post if you want more information.

See I've read both of those, I don't want to say Int8.MyInt = 9; I just want to be able to make my own variable types. I know how to do it with classes and enums, but I want my own integer variable type.

So you want the equivalent of sbyte myInt8 = 9; but with a struct? It sounds to me like you want to overload the = operator which is not possible in C# (although you can overload other operators such as +, - and == ). As far as I know, it isn't possible to create a value type in C#, they are all native to the language.

A struct holds a series of values in its properties, so you access them in the same way as a class, not as a value type, even though the struct itself is stored on the stack.

A good example of a commonly used struct is the DateTime struct which can be found in the System namespace. If you look at the way the DateTime is used, you can do the following:

DateTime myDT = new DateTime();
myDT.Year = 2011;
myDT.Month = 12;
myDT.Day = 30;
myDT.Hour = 10;
myDT.Minute = 9;
myDT.Second = 31;
myDT.Millisecond = 912;
if (myDT.Day == 31 && myDT.Month == 12)
  Console.WriteLine("It's New Year's Eve today! Happy New Year!");

Note that you can't write myDT = "2011/12/30 10:09:31.912"; or something similar, because the compiler doesn't know where to store that string. However, you can do myDT = DateTime.Now.AddDays(1); because the AddDays function returns a DateTime type.

I hope this helps, but please let me know if I can explain further.

Edited 4 Years Ago by darkagn: n/a

Yeah that sounds about right. I was just curious as to how to create my own variable types like Int64, Int32, Int16 so and so forth. But if it's all language based then I don't think there's too much I can do outside of enums, classes, and interfaces.

I think you can't do that with the current C#. Maybe if you can edit the CLR and modify the compiler to create appropriate IL when you use your "own" type then you can manage it, which is almost impossible :D

You can try C++ though!

I'm way out of practice in C++ lol taking a new and improved class on it this semester though.

Although this thread is closed, I feel it needs some additional information:

As far as I know, it isn't possible to create a value type in C#, they are all native to the language.

Structs are value types in C#, and the "simple types" (i.e., the built-in types) are nothing more than aliases to structs like System.Int32. From a developer's perspective, there really isn't much of a difference between these structs and ones you write yourself.

So you want the equivalent of sbyte myInt8 = 9; but with a struct? It sounds to me like you want to overload the = operator which is not possible in C# (although you can overload other operators such as +, - and == ).

While you can't actually overload = in C#, you get the same effect by creating one or more implicit conversion operators.

This question has already been answered. Start a new discussion instead.