Structure in C# allows us to group the variables and methods. Its some what similar to classes but that's not true there are no. of difference between class and structure. But here in this post I am not going to discuss about that, here I am going to explain how to Initialize Structure. Facts:
1. Structure is Value type.
2. D# oesn't allows to create parameter less constructor because its initialize the variable with the default values. Now consider the below case where I have created two structure
structure 1 : with the public members.public struct StructMember
{
public int a;
public int b;
}structure 2 : with the properties.public struct StructProperties
{
private int a;
private int b; public int A
{
get
{ return a; }
set
{ a = value; }
} public int B
{
get
{ return b; }
set
{ b = value; }
}
} Now by considering above two facts in mind and I tried to use the both the structure as below public class MainClass
{
public static void Main()
{
StructMembers MembersStruct; StructProperties PropertiesStruct;
MembersStruct.X = 100;
MembersStruct.Y = 200; PropertiesStruct.X = 100;
PropertiesStruct.Y = 200;
}
}After doing this when I tried to compile the code I received below error the C# compiler issues the following error: error CS0165: Use of unassigned local variable 'PropertiesStruct'So by this C# compiler informs that it allow to use the first structure without an error but not allow to use second structure which exposed property. Read more: Mind Solutions
1. Structure is Value type.
2. D# oesn't allows to create parameter less constructor because its initialize the variable with the default values. Now consider the below case where I have created two structure
structure 1 : with the public members.public struct StructMember
{
public int a;
public int b;
}structure 2 : with the properties.public struct StructProperties
{
private int a;
private int b; public int A
{
get
{ return a; }
set
{ a = value; }
} public int B
{
get
{ return b; }
set
{ b = value; }
}
} Now by considering above two facts in mind and I tried to use the both the structure as below public class MainClass
{
public static void Main()
{
StructMembers MembersStruct; StructProperties PropertiesStruct;
MembersStruct.X = 100;
MembersStruct.Y = 200; PropertiesStruct.X = 100;
PropertiesStruct.Y = 200;
}
}After doing this when I tried to compile the code I received below error the C# compiler issues the following error: error CS0165: Use of unassigned local variable 'PropertiesStruct'So by this C# compiler informs that it allow to use the first structure without an error but not allow to use second structure which exposed property. Read more: Mind Solutions
0 comments:
Post a Comment