Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ public class Example
{
public static void Main()
{
Type t = typeof(String);
Type t = typeof(string);
ShowTypeInfo(t);

t = typeof(List<>);
ShowTypeInfo(t);

var list = new List<String>();
var list = new List<string>();
t = list.GetType();
ShowTypeInfo(t);

Object v = 12;
object v = 12;
t = v.GetType();
ShowTypeInfo(t);

Expand Down
162 changes: 81 additions & 81 deletions snippets/csharp/System/Type/Attributes/attributes1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,99 +9,99 @@ internal struct S

public abstract class Example
{
protected sealed class NestedClass {}
protected sealed class NestedClass { }

public interface INested {}
public interface INested { }

public static void Main()
{
// Create an array of types.
Type[] types = { typeof(Example), typeof(NestedClass),
typeof(INested), typeof(S) };
Type[] types = [typeof(Example), typeof(NestedClass),
typeof(INested), typeof(S)];

foreach (var t in types)
foreach (var t in types)
{
Console.WriteLine("Attributes for type {0}:", t.Name);
Console.WriteLine($"Attributes for type {t.Name}:");

TypeAttributes attr = t.Attributes;
TypeAttributes attr = t.Attributes;

// To test for visibility attributes, you must use the visibility mask.
TypeAttributes visibility = attr & TypeAttributes.VisibilityMask;
switch (visibility)
{
case TypeAttributes.NotPublic:
Console.WriteLine(" ...is not public");
break;
case TypeAttributes.Public:
Console.WriteLine(" ...is public");
break;
case TypeAttributes.NestedPublic:
Console.WriteLine(" ...is nested and public");
break;
case TypeAttributes.NestedPrivate:
Console.WriteLine(" ...is nested and private");
break;
case TypeAttributes.NestedFamANDAssem:
Console.WriteLine(" ...is nested, and inheritable only within the assembly" +
"\n (cannot be declared in C#)");
break;
case TypeAttributes.NestedAssembly:
Console.WriteLine(" ...is nested and internal");
break;
case TypeAttributes.NestedFamily:
Console.WriteLine(" ...is nested and protected");
break;
case TypeAttributes.NestedFamORAssem:
Console.WriteLine(" ...is nested and protected internal");
break;
}
// To test for visibility attributes, you must use the visibility mask.
TypeAttributes visibility = attr & TypeAttributes.VisibilityMask;
switch (visibility)
{
case TypeAttributes.NotPublic:
Console.WriteLine(" ...is not public");
break;
case TypeAttributes.Public:
Console.WriteLine(" ...is public");
break;
case TypeAttributes.NestedPublic:
Console.WriteLine(" ...is nested and public");
break;
case TypeAttributes.NestedPrivate:
Console.WriteLine(" ...is nested and private");
break;
case TypeAttributes.NestedFamANDAssem:
Console.WriteLine(" ...is nested, and inheritable only within the assembly" +
"\n (cannot be declared in C#)");
break;
case TypeAttributes.NestedAssembly:
Console.WriteLine(" ...is nested and internal");
break;
case TypeAttributes.NestedFamily:
Console.WriteLine(" ...is nested and protected");
break;
case TypeAttributes.NestedFamORAssem:
Console.WriteLine(" ...is nested and protected internal");
break;
}

// Use the layout mask to test for layout attributes.
TypeAttributes layout = attr & TypeAttributes.LayoutMask;
switch (layout)
{
case TypeAttributes.AutoLayout:
Console.WriteLine(" ...is AutoLayout");
break;
case TypeAttributes.SequentialLayout:
Console.WriteLine(" ...is SequentialLayout");
break;
case TypeAttributes.ExplicitLayout:
Console.WriteLine(" ...is ExplicitLayout");
break;
}
// Use the layout mask to test for layout attributes.
TypeAttributes layout = attr & TypeAttributes.LayoutMask;
switch (layout)
{
case TypeAttributes.AutoLayout:
Console.WriteLine(" ...is AutoLayout");
break;
case TypeAttributes.SequentialLayout:
Console.WriteLine(" ...is SequentialLayout");
break;
case TypeAttributes.ExplicitLayout:
Console.WriteLine(" ...is ExplicitLayout");
break;
}

// Use the class semantics mask to test for class semantics attributes.
TypeAttributes classSemantics = attr & TypeAttributes.ClassSemanticsMask;
switch (classSemantics)
{
case TypeAttributes.Class:
if (t.IsValueType)
{
Console.WriteLine(" ...is a value type");
}
else
{
Console.WriteLine(" ...is a class");
}
break;
case TypeAttributes.Interface:
Console.WriteLine(" ...is an interface");
break;
}
// Use the class semantics mask to test for class semantics attributes.
TypeAttributes classSemantics = attr & TypeAttributes.ClassSemanticsMask;
switch (classSemantics)
{
case TypeAttributes.Class:
if (t.IsValueType)
{
Console.WriteLine(" ...is a value type");
}
else
{
Console.WriteLine(" ...is a class");
}
break;
case TypeAttributes.Interface:
Console.WriteLine(" ...is an interface");
break;
}

if ((attr & TypeAttributes.Abstract) != 0)
{
Console.WriteLine(" ...is abstract");
}
if ((attr & TypeAttributes.Abstract) != 0)
{
Console.WriteLine(" ...is abstract");
}

if ((attr & TypeAttributes.Sealed) != 0)
{
Console.WriteLine(" ...is sealed");
}
Console.WriteLine();
}
if ((attr & TypeAttributes.Sealed) != 0)
{
Console.WriteLine(" ...is sealed");
}

Console.WriteLine();
}
}
}
// The example displays the following output:
Expand All @@ -128,4 +128,4 @@ public static void Main()
// ...is SequentialLayout
// ...is a value type
// ...is sealed
// </Snippet1>
// </Snippet1>
42 changes: 22 additions & 20 deletions snippets/csharp/System/Type/BaseType/basetype3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@

public class Example
{
public static void Main()
{
foreach (var t in typeof(Example).Assembly.GetTypes()) {
Console.WriteLine("{0} derived from: ", t.FullName);
var derived = t;
do {
derived = derived.BaseType;
if (derived != null)
Console.WriteLine(" {0}", derived.FullName);
} while (derived != null);
Console.WriteLine();
}
}
public static void Main()
{
foreach (var t in typeof(Example).Assembly.GetTypes())
{
Console.WriteLine($"{t.FullName} derived from: ");
var derived = t;
do
{
derived = derived.BaseType;
if (derived != null)
Console.WriteLine($" {derived.FullName}");
} while (derived != null);
Console.WriteLine();
}
}
}

public class A {}
public class A { }

public class B : A
{}
{ }

public class C : B
{}
public class C : B
{ }
// The example displays the following output:
// Example derived from:
// System.Object
//
//
// A derived from:
// System.Object
//
//
// B derived from:
// A
// System.Object
//
//
// C derived from:
// B
// A
Expand Down
6 changes: 3 additions & 3 deletions snippets/csharp/System/Type/BaseType/remarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;


// <Snippet1>
class B<U> { }
Expand All @@ -7,5 +7,5 @@ class C<T> : B<T> { }

class ProgStubClass
{
public static void Main() {}
}
public static void Main() { }
}
4 changes: 2 additions & 2 deletions snippets/csharp/System/Type/BaseType/testbasetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TestType
public static void Main()
{
Type t = typeof(int);
Console.WriteLine("{0} inherits from {1}.", t,t.BaseType);
Console.WriteLine($"{t} inherits from {t.BaseType}.");
}
}
//</Snippet1>
//</Snippet1>
29 changes: 11 additions & 18 deletions snippets/csharp/System/Type/ContainsGenericParameters/source.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//<Snippet1>
using System;
using System.Reflection;
using System.Collections.Generic;



// Define a base class with two type parameters.
public class Base<T, U> { }
Expand All @@ -25,10 +25,10 @@ public static void Main()
"\r\n--- Display a generic type and the open constructed");
Console.WriteLine(" type from which it is derived.");

// Create a Type object representing the generic type definition
// Create a Type object representing the generic type definition
// for the Derived type, by omitting the type argument. (For
// types with multiple type parameters, supply the commas but
// omit the type arguments.)
// omit the type arguments.)
//
Type derivedType = typeof(Derived<>);
DisplayGenericTypeInfo(derivedType);
Expand All @@ -39,25 +39,21 @@ public static void Main()

private static void DisplayGenericTypeInfo(Type t)
{
Console.WriteLine("\r\n{0}", t);
Console.WriteLine($"\r\n{t}");

Console.WriteLine("\tIs this a generic type definition? {0}",
t.IsGenericTypeDefinition);
Console.WriteLine($"\tIs this a generic type definition? {t.IsGenericTypeDefinition}");

Console.WriteLine("\tIs it a generic type? {0}",
t.IsGenericType);
Console.WriteLine($"\tIs it a generic type? {t.IsGenericType}");

Console.WriteLine("\tDoes it have unassigned generic parameters? {0}",
t.ContainsGenericParameters);
Console.WriteLine($"\tDoes it have unassigned generic parameters? {t.ContainsGenericParameters}");

if (t.IsGenericType)
{
// If this is a generic type, display the type arguments.
//
Type[] typeArguments = t.GetGenericArguments();

Console.WriteLine("\tList type arguments ({0}):",
typeArguments.Length);
Console.WriteLine($"\tList type arguments ({typeArguments.Length}):");

foreach (Type tParam in typeArguments)
{
Expand All @@ -66,14 +62,11 @@ private static void DisplayGenericTypeInfo(Type t)
//
if (tParam.IsGenericParameter)
{
Console.WriteLine(
"\t\t{0} (unassigned - parameter position {1})",
tParam,
tParam.GenericParameterPosition);
Console.WriteLine($"\t\t{tParam} (unassigned - parameter position {tParam.GenericParameterPosition})");
}
else
{
Console.WriteLine("\t\t{0}", tParam);
Console.WriteLine($"\t\t{tParam}");
}
}
}
Expand Down
Loading
Loading