- Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
//public int UserId = 10; //Error - A struct may not declare a default constructor (a constructor without parameters) or a destructor. Any private or otherwise inaccessible members can be initialized only in a constructor.
- Copy a struct to struct.
If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance.
Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary.
Student s1 = new Student();
Student s2;
s1 = s2;
Response.Write(s1.PUserId);
Response.Write(s2.PUserId);
Here all the values of s1 will be copied to s2.- Structs are value types and classes are reference types.
- Unlike classes, structs can be instantiated without using a new operator.
Student s1;
s1.Address = "Savadipalayam";- Initialize the fields through properties. Use of unassigned local variable 's1'.
Student s1;
Response.Write(s1.PUserId)
In fact, when instantiating a struct without the new keyword, we must first initialize its fields explicitly.
Student s1;
s1.PUserId = 10;
Response.Write(s1.PUserId)- Structs can declare constructors that have parameters. But all the field variables should be fully initialzed.
public Student(int UserId, int ZipCode, double Salary, string Address)
{
this.UserId = UserId;
this.ZipCode = ZipCode;
this.Salary = Salary;
//this.Address = Address;
}
This will produce an error Field 'StructTest.Student.Address' must be fully assigned before control is returned to the caller". Since Address is not initialized.- A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
- A struct can implement interfaces, and it does that exactly as classes do.
- Cannot convert null to 'StructTest.Student'
Student s2 = null;
DateTime is struct. We can't assign null to DateTime.
Cannot convert null to 'System.DateTime' because it is a non-nullable value type.
eg.
DateTime dt = null;- A struct can be used as a nullable type and can be assigned a null value.
Working with Nullable Types in struct.- If a field is not initialized whether it is a primitive data type it produce the error "Use of possibly unassigned field 'i'" when we didn’t create instance for the struct.
Student1 s3;
Response.Write(s3.i);- When we create an instance for the struct but we don’t have any constructor to assing value, it will asign the default value for premitive datatype and for others it will assing null(null for string).
public struct Student1
{
public int i;
public string j;
}
Student1 s3 = new Student1();
Response.Write(s3.i); //O/P = 0
Response.Write(s3.j); //O/P = null- struct can also have copy constructor but have to be fully initialzed.
public Student(Student stud)
{
this.UserId = stud.UserId;
this.ZipCode = stud.ZipCode;
this.Salary = stud.Salary;
//Field 'StructTest.Student.Address' must be fully assigned before control is returned to the caller
//this.Address = stud.Address;
this.Address = stud.Address;
}- Struct can contain methods. It may be static, and a static method can call only another static methods.
public void sub()
{
}
public static string multiply()
{
return Send();
}
public static string Send()
{
//An object reference is required for the non-static field, method, or property 'StructTest.Student.Address'
//Address = "Mottur Road";
//return Address;
Street = "Mottur Road";
return Street;
}- //The allowed modifiers are new, static, virtual, override, and a valid combination of the four access modifiers (public, internal and private).
// Override the ToString method so the value appears in text
public override string ToString()
{
return String.Format("({0},{1})", Street, Address);
}- Support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties.
public int PUserId
{
get { return UserId; }
set { UserId = value; }
}- Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal.. If we declare a struct inside a namespace with any of the access specifier as private, protected, or protected internal, we will get this error.
So it is always public when we declare a struct inside a namespace.- Abstract and sealed modifiers are not permitted in a struct declaration since it is always implicitly sealed.
Since it is implicitly sealed, struct members may not be declared protected.
So we can't able to inherit another struct.
Since it is not abstract, struct can't be a base to a class or another structure. - Structs are value types and classes are reference types.
struct Student : IOne, ITwo
{
int UserId;
//public int UserId = 10; //Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
public static string Street = "Savadipalayam";
int ZipCode;
double Salary;
public string Address;
//Structs cannot contain explicit parameterless constructors(Default constructor)
/* public Student()
{
this.UserId = 10;
this.ZipCode = 637101;
this.Salary = 2000;
this.Address = "Idappadi";
}
*/
// all the members of the struct has to be initialized in this way
public Student(int UserId, int ZipCode, double Salary, string Address)
{
this.UserId = UserId;
this.ZipCode = ZipCode;
this.Salary = Salary;
this.Address = Address;
}
// struct can also have copy constructor but have to be fully initialzed
public Student(Student stud)
{
this.UserId = stud.UserId;
this.ZipCode = stud.ZipCode;
this.Salary = stud.Salary;
//Field 'StructTest.Student.Address' must be fully assigned before control is returned to the caller
//this.Address = stud.Address;
this.Address = stud.Address;
}
public int PUserId
{
get { return UserId; }
set { UserId = value; }
}
//new protected member declared in struct
//protected void add()
//{
//}
//Struct can contain methods. It may be static, and can call only another static methods.
public void sub()
{
}
public static string multiply()
{
return Send();
}
public static string Send()
{
//An object reference is required for the non-static field, method, or property 'StructTest.Student.Address'
//Address = "Mottur Road";
//return Address;
Street = "Mottur Road";
return Street;
}
//The modifier 'abstract' is not valid for this item
//public abstract void Return() { }
#region ITwo Members
void ITwo.add()
{
throw new System.NotImplementedException();
}
public void sum()
{
throw new System.NotImplementedException();
}
#endregion
#region IOne Members
void IOne.add()
{
throw new System.NotImplementedException();
}
void IOne.sum()
{
throw new System.NotImplementedException();
}
#endregion
}
//Nested Structures
public struct outer
{
public int i;
public struct inner
{
public int j;
}
}
How to call?.
outer.inner inn = new outer.inner();
inn.j = 10;
References
C# School
vijaymukhi
C# Online.net
codeproject
csharp-station