Answer is
Yes – user.UserGroup = Group.Users | Group.Managers;
// see FlagsAttribute
Yes – user.UserGroup = Group.Administrators;
var b = user.UserGroup == Group.Administrators;
// b is true
Yes – user.UserGroup = Group.Supervisors;
var c = user.UserGroup < Group.Administrators;
// c is true because 2 < 8
1) Yes, because Group is enum with FlagAttribute
2) Yes, because only Administrator = 8 and 8 == 8 is true
3) Yes, becaues only Supervisor = 2 and 2 < 8 is true
I typed this code for check whether first case works . In this case it`s not working. The code output NO. Other cases are YES .
public class User
{
public Group UserGroup { get; set; }
}
[FlagsAttribute()]
public enum Group
{
Users=1,
Supervisors=2,
Managers=4,
Administrators=8
}
class Program
{
static void Main(string[] args)
{
// The first case
User us1 = new User();
us1.UserGroup = Group.Supervisors | Group.Users;
bool checkVar = us1.UserGroup==Group.Supervisors;
if (checkVar)
{
Console.WriteLine(“YES”);
}
else { Console.WriteLine(“NO”); }
//The second case
User us2 = new User();
us2.UserGroup = Group.Administrators;
if (us2.UserGroup == Group.Administrators)
{
Console.WriteLine(“YES”);
}
else { Console.WriteLine(“NO”); }
// The third case
User us3 = new User();
us3.UserGroup = Group.Supervisors;
if (us3.UserGroup < Group.Administrators)
{
Console.WriteLine("YES");
}
else { Console.WriteLine("NO"); }
Console.ReadLine();
}
}
Tested with the visiual studio :
User u = new User();
u.UserGroup = Group.Administrator | Group.Users;
Console.WriteLine((u.UserGroup == Group.Administrator).ToString());
User u1 = new User();
u1.UserGroup = Group.Administrator;
Console.WriteLine(u1.UserGroup==Group.Administrator);
User u2 = new User();
u2.UserGroup = Group.Super;
Console.WriteLine(u2.UserGroup< Group.Administrator);
Console.Read();
public class User
{
public UserGroup Group;
}
public class Flags
{
public static void Main(string[] args)
{
User usr = new User()
{
Group = UserGroup.Administrator
};
if (usr.Group == UserGroup.Administrator)
{
Console.WriteLine(“User1 is an administrator”);
}
User usr2 = new User() { Group = UserGroup.Supervisor };
if (usr2.Group < UserGroup.Administrator)
{
Console.WriteLine("User2 is an Supervisor");
}
User usr3 = new User() { Group = UserGroup.Administrator | UserGroup.manager };
if (usr3.Group.HasFlag( UserGroup.manager))
{
Console.WriteLine("User3 is an Manager");
}
The answer should be
No
Yes
Yes
I take it back. The answer given is correct.
It should be
Yes
No
No
Answer is
Yes – user.UserGroup = Group.Users | Group.Managers;
// see FlagsAttribute
Yes – user.UserGroup = Group.Administrators;
var b = user.UserGroup == Group.Administrators;
// b is true
Yes – user.UserGroup = Group.Supervisors;
var c = user.UserGroup < Group.Administrators;
// c is true because 2 < 8
Three yeses are correct. Tried with simple console application.
How can all answer are correct?
can you send me the console code?
Yes
Yes
Yes
Tested by console
三个答案都是正确的。
Can someone explain the second one please, why is that “NO”.
Um den Namespace anzugeben müsste es wie folgt aussehen: “[DataContract(Namespace=”Individual”)]”
Because with “[DataContract (Name =” Individual “)]” is specified name.
To specify the namespace, it would look like this: “[DataContract (Namespace =” Individual “)]”
No,
Yes,
Yes.
Yes, Yes, Yes.
1) Yes, because Group is enum with FlagAttribute
2) Yes, because only Administrator = 8 and 8 == 8 is true
3) Yes, becaues only Supervisor = 2 and 2 < 8 is true
YES -> this one is confusing. user can be part of new group with enum 3(Group.Users | Group.Supervisors)
YES
YES
Please can any one give the correct answer to me?
Above comments are creating confusing.
Yes,
Yes,
Yes,
all checked in Visual Studio 2015.
I typed this code for check whether first case works . In this case it`s not working. The code output NO. Other cases are YES .
public class User
{
public Group UserGroup { get; set; }
}
[FlagsAttribute()]
public enum Group
{
Users=1,
Supervisors=2,
Managers=4,
Administrators=8
}
class Program
{
static void Main(string[] args)
{
// The first case
User us1 = new User();
us1.UserGroup = Group.Supervisors | Group.Users;
bool checkVar = us1.UserGroup==Group.Supervisors;
if (checkVar)
{
Console.WriteLine(“YES”);
}
else { Console.WriteLine(“NO”); }
//The second case
User us2 = new User();
us2.UserGroup = Group.Administrators;
if (us2.UserGroup == Group.Administrators)
{
Console.WriteLine(“YES”);
}
else { Console.WriteLine(“NO”); }
// The third case
User us3 = new User();
us3.UserGroup = Group.Supervisors;
if (us3.UserGroup < Group.Administrators)
{
Console.WriteLine("YES");
}
else { Console.WriteLine("NO"); }
Console.ReadLine();
}
}
For true test first case you must check
if(us1.UserGroup == Group.Supervisors && us1.UserGroup == Group.Users)
but result not changed.
Please go to the internet sites we stick to, including this one, because it represents our picks from the web.
Yes, No, No ?
Yes – Clear
No – The default namespace is “http://tempuri.org”
Yes – It will have a default value even if is null
3 NO. I check in VS. Field with [DataMember(EmitDefaultValue = false)] was skipped with default value (null for string).
Tested with the visiual studio :
User u = new User();
u.UserGroup = Group.Administrator | Group.Users;
Console.WriteLine((u.UserGroup == Group.Administrator).ToString());
User u1 = new User();
u1.UserGroup = Group.Administrator;
Console.WriteLine(u1.UserGroup==Group.Administrator);
User u2 = new User();
u2.UserGroup = Group.Super;
Console.WriteLine(u2.UserGroup< Group.Administrator);
Console.Read();
Got answer :
False
True
True
means
NO
YES
YES
yes no no
Yes, Yes, Yes
using System;
[Flags]
public enum UserGroup
{
Users = 1,
Supervisor = 2,
manager = 4,
Administrator = 8
}
public class User
{
public UserGroup Group;
}
public class Flags
{
public static void Main(string[] args)
{
User usr = new User()
{
Group = UserGroup.Administrator
};
if (usr.Group == UserGroup.Administrator)
{
Console.WriteLine(“User1 is an administrator”);
}
User usr2 = new User() { Group = UserGroup.Supervisor };
if (usr2.Group < UserGroup.Administrator)
{
Console.WriteLine("User2 is an Supervisor");
}
User usr3 = new User() { Group = UserGroup.Administrator | UserGroup.manager };
if (usr3.Group.HasFlag( UserGroup.manager))
{
Console.WriteLine("User3 is an Manager");
}
Console.ReadKey();
}
}